Change the foreground of a datagrid cell

你。 提交于 2021-02-11 12:16:04

问题


I open my datagrid, showing numerical values in black on some columns, the same data are written in red on other columns, just to say no values are really present there, hasn't been edited. If someone change the numbers in red they should became black.

I have write in xaml one style for the DatagridCell -

<Window.Resources>
    <Style x:Key="D3ForegroundStyle" TargetType="{x:Type DataGridCell}" >
        <Setter Property="Foreground" Value="Black" />
        <Style.Triggers>
            <DataTrigger Binding="{Binding Path=D3StatusVal}" Value="inherited">
                <Setter Property="Foreground" Value="Red" />
            </DataTrigger>
        </Style.Triggers>
    </Style>
</Window.Resources>

D3StatusVal it's a string which value could be "normal" or "inherited". The style is linked to column in this way:

                <DataGridTextColumn.CellStyle>
                    <Style TargetType="{x:Type DataGridCell}" BasedOn="{StaticResource D3ForegroundStyle}"/>
                </DataGridTextColumn.CellStyle>

the first time, soon after I open the window, datagrid has been filled in a proper manner as I like, accordingly with the value of D3StatusVal, some values are shown black other red. After this if I try to change the data of the cells nothing happens (I mean with the cell color), even though the value of D3StatusVal is changed, and one event of PropertyChanged (with the x:name of column) is called Thanks in advance.

No any hint at regards?! Ok, I would pose the question in a more general manner. Let say, I would like to change programmatically the foreground of a cell every time I change its contents (not dependent of value write in cell), what's the correct method to do it?


回答1:


Have you tried using DynamicResouce instead of StaticResource




回答2:


First, apply change notification to your D3StatusVal property by implementing INotifyPropertyChanged.

BasedOn wont work if you set target type to DataGridCell explicitly. Instead change your style to :

<Style x:Key="D3ForegroundStyle">
    <Setter Property="DataGridCell.Foreground" Value="Black" />
    <Style.Triggers>
        <DataTrigger Binding="{Binding Path=D3StatusVal}" Value="inherited">
            <Setter Property="DataGridCell.Foreground" Value="Red" />
        </DataTrigger>
    </Style.Triggers>
</Style>

Or apply the style as :

        <DataGrid.CellStyle>
            <Style TargetType="DataGridCell">
               <Setter Property="Foreground" Value="Black" />
               <Style.Triggers>
                  <DataTrigger Binding="{Binding Path=D3StatusVal}" Value="inherited">
                    <Setter Property="Foreground" Value="Red" />
                 </DataTrigger>
               </Style.Triggers>
            </Style>
        </DataGrid.CellStyle>

To change color only in particular column, use MultiDataTrigger to check Column.Header value (eg below uses Age Column) :

           <MultiDataTrigger>
                <MultiDataTrigger.Conditions>
                    <Condition Binding="{Binding Path=Age}" Value="12"/>
                    <Condition Binding="{Binding Column.Header, RelativeSource={RelativeSource Self}}" Value="Age"/>
                </MultiDataTrigger.Conditions>
                <MultiDataTrigger.Setters>
                    <Setter Property="DataGridCell.Foreground" Value="Yellow" />
                </MultiDataTrigger.Setters>
            </MultiDataTrigger>


来源:https://stackoverflow.com/questions/38720218/change-the-foreground-of-a-datagrid-cell

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!