我在Xamarin Forms中绑定有问题。根据Listview
的计数项目,我想将的IsVisible
财产设置为true/false
。如果Listview
有任何项目,Label IsVisible
将为false
,否则将为true
。有没有可能在Xamarin表单中进行绑定?我试图做到这一点,但我不知道如何在XAML中将0
号码转换为boolean false
。绑定列表视图项目计数到Xamarin表单中的标签可见性
2
A
回答
2
当然有可能。我假设你已经有了一种获取ListView中项目数的方法,所以这里是如何将这个数字转换为XAML中的布尔值。
您需要的是IValueConverter接口的自定义实现。它可以将绑定提供的值转换为其他内容并在需要时返回。
在你的情况,你想要一个整数并返回一个布尔值。如果该值为零,则会返回true,否则返回false。
public class CountToBoolenConverter : IValueConverter
{
public object Convert (object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return (int)value != 0;
}
public object ConvertBack (object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
接下来,您需要在XAML中定义命名空间。在文件的顶部,你应该添加这样的事情:
xmlns:local="clr-namespace:YourNameSpace;assembly=YourAssembly"
添加的转换器的页面资源刚刚开幕<ContentPage>
标签之后(或者你有什么类型的页面):
<ContentPage>
<ContentPage.Resources>
<ResourceDictionary>
<CountToBoolenConverter x:Key="countToBoolean" />
</ResourceDictionary>
</ContentPage.Resources>
<!-- Rest of the page -->
而且然后最后使用转换器的可见性属性设置为您的标签:
<Label IsVisible="{Binding ItemCount, Converter={StaticResource countToBoolean}}">
ItemCount中是在你的视图模型整数属性,它应该包含的数ListView项目。您可能已经有了一种为ListView加载项目集合的方法,因此应该弄清楚该属性应该如何完成。
1
你可以纯粹用XAML做一个DataTrigger:
<?xml version="1.0" encoding="utf-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="ListViewTriggerToHideLabel.MainPage">
<StackLayout>
<Label Text="Welcome to Xamarin Forms!" IsVisible="False">
<Label.Triggers>
<DataTrigger TargetType="Label"
Binding="{Binding Source={x:Reference TheListView}, Path=ItemsSource.Count}"
Value="0">
<Setter Property="IsVisible" Value="True" />
</DataTrigger>
</Label.Triggers>
</Label>
<ListView x:Name="TheListView" />
<Button Text="Add an item" Clicked="Button_OnClicked" />
</StackLayout>
</ContentPage>
代码隐藏处理按钮点击并初始化列表中的内容(我通常使用data binding,但在我的例子简单使用代码隐藏):
using System;
using System.Collections.ObjectModel;
using Xamarin.Forms;
namespace ListViewTriggerToHideLabel {
public partial class MainPage : ContentPage {
private readonly ObservableCollection<string> _items = new ObservableCollection<string>();
public MainPage() {
InitializeComponent();
TheListView.ItemsSource = _items;
}
private void Button_OnClicked(object sender, EventArgs e) {
_items.Add("Ouch");
}
}
}
到Count属性的绑定工作,因为ItemsSource是ObservableCollection。
相关问题
- 1. Xamarin表单列表视图绑定
- 2. WPF列表视图性能 - 静态与数据绑定项目
- 3. Xamarin表单绑定属性标签的文本
- 4. Xamarin表单 - 绑定到ControlTemplate
- 5. 如何在列表视图中更改线性布局的项目可见性
- 6. 将列表绑定到列表视图
- 7. 将本地数组的值绑定到所有列表框项目的特定属性(可见性)
- 8. 如何获取Xamarin表单中标签绑定值的值
- 9. 如何获取Xamarin android阵列适配器中的可见列表项计数
- 10. Android:改变所有列表视图行中项目的可见性
- 11. 如何阅读JQuery中的列表视图项目(标签)
- 12. 将列表框中控件的可见性绑定到项目源集合中不包含的属性
- 13. Xamarin表单列表视图行宽
- 14. Xamarin表单 - 定期列表视图数据刷新
- 15. Xamarin表单绑定属性不显示
- 16. 指定唯一标签列表中的项目列表
- 17. 如何保持列表视图的选定项目WPF绑定
- 18. 带有列表视图和图像列表的项目图标
- 19. Android的列表视图中不可见
- 20. 数据列表标签的php项目
- 21. 水平列表视图导航 - 可见项目
- 22. 将XAML中的可见性绑定到可见性属性
- 23. 嵌套列表视图数据绑定 - 属性未找到?
- 24. 将ResourceBundle属性绑定到列表项
- 25. Xamarin表单按钮绑定
- 26. Xamarin表单绑定嵌套
- 27. 如何将标签绑定到Xamarin中的属性?
- 28. 滚动,但直到标题可见在Android的列表视图
- 29. 这是一个很好的设计绑定到视图模型的可见性
- 30. 将属性绑定到自定义列表项目
您可以分享您迄今为止编写的代码吗?在XAML和代码背后 –