C# ItemSource的Xamarin绑定计数
我需要使用XAML启用/禁用,XAML是一个基于ItemsSource中元素数量的选择器C# ItemSource的Xamarin绑定计数,c#,xamarin,xamarin.forms,picker,C#,Xamarin,Xamarin.forms,Picker,我需要使用XAML启用/禁用,XAML是一个基于ItemsSource中元素数量的选择器 我曾尝试使用WoSpesaDett.DsTecnico.Count>0,但它不起作用 我怎样才能做到这一点 谢谢大家! 您可以在绑定上下文中创建bool: public bool PickerShouldBeEnabled { get { return WoSpesaDett.DsTecnico.Count > 0; } //returns true if there are more t
我曾尝试使用WoSpesaDett.DsTecnico.Count>0
,但它不起作用
我怎样才能做到这一点
谢谢大家! 您可以在绑定上下文中创建bool:
public bool PickerShouldBeEnabled
{
get { return WoSpesaDett.DsTecnico.Count > 0; } //returns true if there are more than 0 elements
}
或者,如果您只想在列表中有任何元素时启用它,则可以使用linq“Any()”,以获得更好的性能
public bool PickerShouldBeEnabled
{
get { return WoSpesaDett.DsTecnico.Any(); } //returns true if there are any elements
}
或者,您可以创建一个以列表为值并基于列表元素计数返回true的。
我也可以为这种情况提供一个基本的转换器。您可以在绑定上下文中创建bool:
public bool PickerShouldBeEnabled
{
get { return WoSpesaDett.DsTecnico.Count > 0; } //returns true if there are more than 0 elements
}
或者,如果您只想在列表中有任何元素时启用它,则可以使用linq“Any()”,以获得更好的性能
public bool PickerShouldBeEnabled
{
get { return WoSpesaDett.DsTecnico.Any(); } //returns true if there are any elements
}
或者,您可以创建一个以列表为值并基于列表元素计数返回true的。
对于这种情况,我也可以为您提供一个基本的转换器。
IValueConverter
对于整数到布尔:
public class IntToBooleanConverter : IValueConverter
{
public object Convert (object value, Type targetType, object parameter, CultureInfo culture)
{
int minimumLength = System.Convert.ToInt32 (parameter);
return (int)value >= minimumLength;
}
public object ConvertBack (object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException ();
}
}
IValueConverter
用于整数到布尔值:
public class IntToBooleanConverter : IValueConverter
{
public object Convert (object value, Type targetType, object parameter, CultureInfo culture)
{
int minimumLength = System.Convert.ToInt32 (parameter);
return (int)value >= minimumLength;
}
public object ConvertBack (object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException ();
}
}
注意:
如果您只需要更改选择器的可见性一次(而不是以动态方式),请创建一个转换器,就像其他答案所指出的那样
否则:
理论上,动态隐藏或显示UI控件非常简单。您所要做的就是在模型中引入一个布尔属性,该属性应如下所示,例如:
public bool MyPickerShouldBeVisible => WoSpesaDett.DsTecnico.Count > 0;
现在的问题是,您需要通知您的视图
有关MyPickerShouldBeVisible
的更改。我通常使用来处理INotifyPropertyChange
之类的东西。使用它,您可以使用一个特殊的属性来标记您的DsTecnico
属性(nameof(mypickersouldbevisible)),以使此解决方案起作用
下面是一个完整的示例,ViewModel数据被简化:
// INotifyPropertyChanged should be handled by `Fody.PropertyChanged`
public class MyViewModel : INotifyPropertyChanged
{
public IList<string> MyData { get; set; }
[AlsoNotifyFor(nameof(MyPickerShouldBeVisible))]
public bool ShouldShowPicker => MyData.Any();
}
//INotifyPropertyChanged应该由`Fody.PropertyChanged'处理`
公共类MyViewModel:INotifyPropertyChanged
{
公共IList MyData{get;set;}
[还包括(姓名(MyPickerShouldBeVisible))]
public bool ShouldShowPicker=>MyData.Any();
}
使用上面的示例,将导致选择器的动态行为。注意:
如果您只需要更改选择器的可见性一次(而不是以动态方式),请创建一个转换器,就像其他答案所指出的那样
否则:
理论上,动态隐藏或显示UI控件非常简单。您所要做的就是在模型中引入一个布尔属性,该属性应如下所示,例如:
public bool MyPickerShouldBeVisible => WoSpesaDett.DsTecnico.Count > 0;
现在的问题是,您需要通知您的视图
有关MyPickerShouldBeVisible
的更改。我通常使用来处理INotifyPropertyChange
之类的东西。使用它,您可以使用一个特殊的属性来标记您的DsTecnico
属性(nameof(mypickersouldbevisible)),以使此解决方案起作用
下面是一个完整的示例,ViewModel数据被简化:
// INotifyPropertyChanged should be handled by `Fody.PropertyChanged`
public class MyViewModel : INotifyPropertyChanged
{
public IList<string> MyData { get; set; }
[AlsoNotifyFor(nameof(MyPickerShouldBeVisible))]
public bool ShouldShowPicker => MyData.Any();
}
//INotifyPropertyChanged应该由`Fody.PropertyChanged'处理`
公共类MyViewModel:INotifyPropertyChanged
{
公共IList MyData{get;set;}
[还包括(姓名(MyPickerShouldBeVisible))]
public bool ShouldShowPicker=>MyData.Any();
}
使用上面的示例,将导致选择器的动态行为。您可以尝试。您可以尝试。