如何从代码隐藏中更改DataTemplate中的字体颜色?

时间:2022-11-20 21:01:34

I have a XAML DataTemplate like this being applied to ListBoxItems. Notice the grid near the bottom. What I would like to do is change the color of the bound text based on the value of the text, as in 'Status: ERROR' where 'ERROR' is red.

我有一个像这样的XAML DataTemplate应用于ListBoxItems。注意底部附近的网格。我想要做的是根据文本的值更改绑定文本的颜色,如'Status:ERROR',其中'ERROR'为红色。

<DataTemplate x:Key="ItemTemplate">
    <Border BorderBrush="Gray" BorderThickness="1">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="3*"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <Rectangle Grid.Row="0" Grid.Column="0">
                <Rectangle.Fill>
                    <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
                        <GradientStop Color="#FFA9A9A9" Offset="1.0"/>
                        <GradientStop Color="#FFF4F4F5" Offset="0.0"/>
                    </LinearGradientBrush>
                </Rectangle.Fill>
            </Rectangle>
            <Label Content="{Binding Name}" FontSize="22" Grid.Row="0" Grid.Column="0" VerticalAlignment="Center"/>
            <Rectangle Grid.Row="1" Grid.Column="0"  Grid.RowSpan="3">
                <Rectangle.Fill>
                    <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
                        <GradientStop Color="#FFF0F0F1" Offset="1.0"/>
                        <GradientStop Color="#FFF4F4F5" Offset="0.0"/>
                    </LinearGradientBrush>
                </Rectangle.Fill>
            </Rectangle>
            <Grid Grid.Row="1" Grid.Column="0">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="2*"/>
                </Grid.ColumnDefinitions>
                <Label Content="Status:"/>
                <Label Content="{Binding Status}" Grid.Column="1"/>
            </Grid>
            <Label Content="{Binding Bookmark}" ContentStringFormat="Last Bookmark: {0}" Grid.Row="3" Grid.Column="0"/>
        </Grid>
    </Border>
</DataTemplate>

How do I change the color of the text text in that bound Status <Label> from code behind (C#)?

如何从后面的代码(C#)更改绑定的Status

(Or, if not from code behind, how in XAML? Status can have 3 text values (Queued, Running, and Error) and each is matched with an assigned color (Blue, Green, and Red))

(或者,如果不是来自代码,那么XAML中的状态如何?状态可以有3个文本值(排队,运行和错误),每个值都与指定的颜色(蓝色,绿色和红色)相匹配)

1 个解决方案

#1


0  

Based on @SLaks hint, I was able to work out the complete answer. Here it is for others:

根据@SLaks提示,我能够找到完整的答案。这是给其他人的:

This:

这个:

<Grid Grid.Row="1" Grid.Column="0">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="2*"/>
    </Grid.ColumnDefinitions>
    <Label Content="Status:"/>
    <Label Content="{Binding Status}" Grid.Column="1"/>
</Grid>

Becomes this:

变成这样:

<Grid Grid.Row="1" Grid.Column="0">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="2*"/>
    </Grid.ColumnDefinitions>
    <Label Content="Status:"/>
    <Label Content="{Binding Status}" Grid.Column="1">
        <Label.Style>
            <Style TargetType="Label">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Status}" Value="RUNNING">
                        <Setter Property="Label.Foreground" Value="Green"></Setter>
                    </DataTrigger>
                    <DataTrigger Binding="{Binding Status}" Value="ERROR">
                        <Setter Property="Label.Foreground" Value="Red"></Setter>
                    </DataTrigger>
                    <DataTrigger Binding="{Binding Status}" Value="QUEUED">
                        <Setter Property="Label.Foreground" Value="Blue"></Setter>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Label.Style>
    </Label>
</Grid>

#1


0  

Based on @SLaks hint, I was able to work out the complete answer. Here it is for others:

根据@SLaks提示,我能够找到完整的答案。这是给其他人的:

This:

这个:

<Grid Grid.Row="1" Grid.Column="0">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="2*"/>
    </Grid.ColumnDefinitions>
    <Label Content="Status:"/>
    <Label Content="{Binding Status}" Grid.Column="1"/>
</Grid>

Becomes this:

变成这样:

<Grid Grid.Row="1" Grid.Column="0">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="2*"/>
    </Grid.ColumnDefinitions>
    <Label Content="Status:"/>
    <Label Content="{Binding Status}" Grid.Column="1">
        <Label.Style>
            <Style TargetType="Label">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Status}" Value="RUNNING">
                        <Setter Property="Label.Foreground" Value="Green"></Setter>
                    </DataTrigger>
                    <DataTrigger Binding="{Binding Status}" Value="ERROR">
                        <Setter Property="Label.Foreground" Value="Red"></Setter>
                    </DataTrigger>
                    <DataTrigger Binding="{Binding Status}" Value="QUEUED">
                        <Setter Property="Label.Foreground" Value="Blue"></Setter>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Label.Style>
    </Label>
</Grid>