简体   繁体   中英

How can I change the font color in a cell of a WPF ListView depending on the value of the cell?

<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.

Thanks boffins.

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

See also Data Templating Overview on 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM