我有以下rangeobservablecollection:
private readonly RangeObservableCollection<coll> _coll;
其中coll是我要添加到此集合的一堆复选框。我想更改特定添加的复选框的前景色,如下所示:
_coll.Add( info );
有没有办法改变这个颜色?
XAML代码:
<StackPanel Orientation="Horizontal">
<CheckBox Margin="0,0,3,0" Foreground="{Binding Foreground"}">
<CheckBox.IsChecked>
<Binding Path="IsSelected"
Mode="TwoWay">
<Binding.RelativeSource>
<RelativeSource Mode="Parent" />
</Binding.RelativeSource>
</Binding>
</CheckBox.IsChecked>
</CheckBox>
<ContentPresenter />
</StackPanel>
答案 0 :(得分:4)
您可以在ui对象上使用模板将前景颜色绑定到coll类上的SolidColorBrush属性。
<ListBox ItemsSource="{Binding items}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding IsChecked, Mode=TwoWay}" />
<TextBlock Foreground="{Binding Foreground}" Content="{Binding Description}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
您的类定义将如下所示:
public class coll
{
public IsChecked { get; set; }
public string Description { get; set; }
public SolidColorBrush Foreground { get; set; }
}
然后你可以改变前景:
info.Foreground = new SolidColorBrush(Colors.Red);
_coll.Add( info );
你可以在这里使用很多变化来达到同样的效果:转换器,INPC ......