我需要根据可见性的顺序更改 DataGrid 中矩形的颜色。例如,如果我有 5 行,并且如果第 3 行的矩形第一次变得可见,则应该将其填充为绿色。下一个可见的矩形应该是红色的,然后……
这是我的代码:
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Rectangle x:Name="colorBox" Height="15" Width="15" Stroke="#9C9C9C"
Visibility="{Binding Path=IsDisplayable,
Converter={StaticResource BoolVisibilityConverter}}">
<Rectangle.Style>
<Style TargetType="{x:Type Rectangle}">
<Style.Triggers>
<Trigger Property="Visibility" Value="Visible">
<Setter Property="Fill" Value="{Binding Path=FillColor}" />
</Trigger>
</Style.Triggers>
</Style>
</Rectangle.Style>
</Rectangle>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
我已经编写了这段代码,但它不起作用。从逻辑上讲,似乎没有错误,但实际上我认为当矩形的可见性发生变化时,这个触发器永远不会被调用。请给我一个很好的解决这个问题的方法。谢谢
IsDisplayable 属性在此处更新:
<DataGridTemplateColumn x:Name="CheckboxColumnHeader"
Header="{Binding Source={x:Reference Name=treeView}, Path=DataContext.CheckboxColumnHeader}"
IsReadOnly="True" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox x:Name="displayedObjects" IsChecked="{Binding IsDisplayable, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
</CheckBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellStyle>
<Style TargetType="{x:Type DataGridCell}" BasedOn="{StaticResource {x:Type DataGridCell}}">
<Setter Property="HorizontalContentAlignment" Value="Center" />
</Style>
</DataGridTemplateColumn.CellStyle>
</DataGridTemplateColumn>
最后,下面是代码:
public bool IsDisplayable
{
get
{
return this.isDisplayable;
}
set
{
if (this.isDisplayable!= value)
{
this.isDisplayable= value;
//NotifyOfPropertyChange(() => this.isDisplayable);
Action notify = () => NotifyOfPropertyChange(() => this.isDisplayable);
Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle, notify);
}
}
}
public Brush FillColor
{
get
{
return signalColors[currentColorIndex];
}
set
{
}
}