更新可见性更改触发器上的填充颜色

时间:2012-08-08 13:13:56

标签: wpf triggers visibility

我需要根据Visibility的顺序更改DataGrid中Rectangle的颜色。例如,如果我有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
        {
        }
    }

1 个答案:

答案 0 :(得分: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>