How do I get the default color of the text in a WPF control? I'm data-binding the foreground color brush and only want to be able to change color under a certain condition.
A simple example is to color the cell red when the value is 7 for the second column. If I don't know the default color of the text, it is slightly off from the other cells. In the picture below, the default brush is blueish. I use Bushes.Black
for the non 7 value.
The C# code for the data binding line is
public Brush brush
{
get
{
if (data2 == 7)
return Brushes.Red;
return Brushes.Black;
}
}
The XAML code is below.
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<ListView Grid.Row="0" Grid.Column="0" Margin="10,20,10,10" ItemsSource="{Binding data}"
Name="DataList">
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn Width="150" DisplayMemberBinding="{Binding Path=data1}">
<GridViewColumn.Header>
<GridViewColumnHeader Tag="Data1">
<TextBlock>Data 1</TextBlock>
</GridViewColumnHeader>
</GridViewColumn.Header>
</GridViewColumn>
<GridViewColumn Width="150">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock x:Name="Txt2" Text="{Binding data2}" Foreground="{Binding brush}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
<GridViewColumn.Header>
<GridViewColumnHeader Tag="Data2">
<TextBlock>Data 2</TextBlock>
</GridViewColumnHeader>
</GridViewColumn.Header>
</GridViewColumn>
<GridViewColumn Width="150" DisplayMemberBinding="{Binding Path=data3}">
<GridViewColumn.Header>
<GridViewColumnHeader Tag="Data3">
<TextBlock>Data 3</TextBlock>
</GridViewColumnHeader>
</GridViewColumn.Header>
</GridViewColumn>
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
</Grid>