我有一个ObservableCollection<MyEntity>
并且MyEntity
有一个IsChecked
带有PropertyChanged
事件的属性。我有一个Button
并且我想将IsEnabled
属性更改为true
当至少其中一个MyEntity
被MyObservableCollection
选中时。我创建了一个转换器,它至少在检查ObservableCollection
a 时接受并返回 true 。MyEntity
但是return "null"
被退回了。怎么了 ?感谢您的帮助。
XAML
<Window.Resources>
<CollectionViewSource x:Key="MyObservableCollection"/>
<src:MyConverter x:Key="MyConverter"/>
</Window.Resources>
<Button IsEnabled="{Binding Converter={StaticResource MyConverter}, Source={StaticResource MyObservableCollection}}"/>
C# 转换器
class MyConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (null == value)
return "null";
ReadOnlyObservableCollection<object> items = (ReadOnlyObservableCollection<object>)value;
List<MyEntity> myEntities = (from i in items select (MyEntity)i).ToList();
foreach (MyEntity entity in myEntities)
{
if (entity.IsChecked)
{
return true;
}
}
return false;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new System.NotImplementedException();
}
}