<Button Content="1" Background="{Binding HotNumbers, Converter={StaticResource BrushConverter} }"/>
<Button Content="2" Background="{Binding HotNumbers, Converter={StaticResource BrushConverter} }"/>
..
我有 10 个按钮。我正在尝试将每个的背景颜色绑定到ObservableCollection<bool>
. 我尝试使用 aIValueConverter
将布尔值转换为画笔颜色,如下所示。
但是整个集合似乎是在对象值中传递的,而不是在转换器中引发异常的单个项目。
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if ((bool)value)
{
return Brushes.Red;
}
else
{
return Brushes.White;
}
}
有没有其他方法可以做到这一点,这样我就不必为每个按钮创建 10 个不同的属性。