1
votes

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. Slightly Different Color

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

1 Answers

1
votes

only want to be able to change color under a certain condition

Then you should use a trigger in a style applied to the control, so that the property value will be set only under that "certain condition". For example:

<GridViewColumn Width="150">
    <GridViewColumn.CellTemplate>
        <DataTemplate>
            <TextBlock x:Name="Txt2" Text="{Binding data2}">
                <TextBlock.Style>
                    <Style TargetType="TextBlock">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding data2}" Value="7">
                                <Setter Property="Foreground" Value="Red"/>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </TextBlock.Style>
            <TextBlock/>
        </DataTemplate>
    </GridViewColumn.CellTemplate>
    <GridViewColumn.Header>
        <GridViewColumnHeader Tag="Data2">
            <TextBlock>Data 2</TextBlock>
        </GridViewColumnHeader>
    </GridViewColumn.Header>
</GridViewColumn>

Alternatively, bind to a property using a converter and return Binding.DoNothing when you don't want to modify the default value. Converters can be useful when dealing with more complicated scenarios. Given the information in your question so far, the above should work fine.