PropertyChanged.Fody 。我有一个ObservableCollection充满自定义类项目。它们每个都有一个IsTableSelected属性。
如果集合中至少一个项目的IsTableSelected属性设置为true,我希望启用一个按钮。问题是:我的Converter将集合作为值,并且如果集合中的项目更改了属性,则不会被调用。
这是我的XAML:
<Button
Grid.Row="1"
Content="Connect"
Margin="5"
IsEnabled="{Binding Tables, Converter={StaticResource selectedItemsToBoolConverter}}"
/>
这是物品类别:
public class ListBoxItemModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged = (sender, e) => { };
public bool IsTableSelected
{
get;
set;
}
public string Name
{
get;
set;
}
}
这里是转换器:
class SelectedItemsToBoolConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
ObservableCollection<ListBoxItemModel> collection = value as ObservableCollection<ListBoxItemModel>;
if (collection.Count > 0)
{
if (collection.Where(item => item.IsTableSelected == true).ToList().Count > 0)
{
return true;
}
else
{
return false;
}
}
else return false;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
以防万一ViewModel中有一行:
public ObservableCollection<ListBoxItemModel> Tables { get; set; }
= new ObservableCollection<ListBoxItemModel>();
使用MVVM的任何方式都会有所帮助。