如何根据单元格的值更改WPF ListView单元格中的字体颜色?

时间:2021-09-11 20:25:05
<ListView ItemsSource="{Binding}" Name="myView">
            <ListView.View>
                <GridView>
                    <GridViewColumn DisplayMemberBinding="{Binding fieldA}" Header="Field A" />
                    <GridViewColumn DisplayMemberBinding="{Binding fieldB}" Header="Field B" />                    </GridView>
            </ListView.View>
        </ListView>

I would like to know how to format my list view so that if object.fieldA == "apples", the font is red in the fieldA cell.

我想知道如何格式化我的列表视图,以便如果object.fieldA ==“apples”,则fieldA单元格中的字体为红色。

Thanks boffins.

谢谢boffins。

1 个解决方案

#1


6  

You need to use a DataTemplate to show the fieldA value and add a trigger changing the Foreground property for a given value.

您需要使用DataTemplate显示fieldA值并添加触发器以更改给定值的Foreground属性。

See also Data Templating Overview on MSDN.

另请参阅MSDN上的数据模板概述。

<GridViewColumn Header="Field A">
    <GridViewColumn.CellTemplate>
        <DataTemplate>
            <TextBlock x:Name="Txt" Text="{Binding fieldA}" />
            <DataTemplate.Triggers>
                <DataTrigger Binding="{Binding fieldA}" Value="apples">
                    <Setter TargetName="Txt" Property="Foreground" Value="Red" />
                </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>
    </GridViewColumn.CellTemplate>
</GridViewColumn>

#1


6  

You need to use a DataTemplate to show the fieldA value and add a trigger changing the Foreground property for a given value.

您需要使用DataTemplate显示fieldA值并添加触发器以更改给定值的Foreground属性。

See also Data Templating Overview on MSDN.

另请参阅MSDN上的数据模板概述。

<GridViewColumn Header="Field A">
    <GridViewColumn.CellTemplate>
        <DataTemplate>
            <TextBlock x:Name="Txt" Text="{Binding fieldA}" />
            <DataTemplate.Triggers>
                <DataTrigger Binding="{Binding fieldA}" Value="apples">
                    <Setter TargetName="Txt" Property="Foreground" Value="Red" />
                </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>
    </GridViewColumn.CellTemplate>
</GridViewColumn>