0

我需要根据可见性的顺序更改 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
        {
        }
    }
4

1 回答 1

0

你可以完全摆脱你的触发器......

      <DataGridTemplateColumn.CellTemplate>
             <DataTemplate>
                <Rectangle x:Name="colorBox" Height="15" width="15"
                           Fill="{Binding Path=FillColor}"
                           Visibility="{Binding Path=IsDisplayable, 
                                 Converter={StaticResource BoolVisibilityConverter}}"
                 />
             </DataTemplate>
      </DataGridTemplateColumn.CellTemplate>

假设FillColor属性是类型Brush而不是Color!如果它是一种Color类型,则应用ColorToBrushConverter...

这可以在CodePlex的扩展工具包中找到...

    <Window .... 
          xmlns:Toolkit="clr-namespace:Microsoft.Windows.Controls.Core.Converters; assembly=WPFToolkit.Extended"
          ...>
          <DataGrid ...>
               ....

               <DataGridTemplateColumn.CellTemplate>
                  <DataTemplate>
                     <DataTemplate.Resources>
                          <Toolkit:ColorToSolidColorBrushConverter 
                             x:Key="colorToSolidBrushConverter" /> 
                     <DataTemplate.Resources> 
                     <Rectangle x:Name="colorBox" Height="15" width="15"
                           Fill="{Binding Path=FillColor, 
                                Converter={StaticResource colorToSolidBrushConverter}}"
                           Visibility="{Binding Path=IsDisplayable, 
                                 Converter={StaticResource BoolVisibilityConverter}}"
                      />
                 </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
                ....
          </DataGrid>
   </Window> 
于 2012-08-08T13:34:25.737 回答