如何从代码后面更改DataTemplate中的字体颜色?

时间:2015-04-12 13:04:00

标签: c# xaml datatemplate

我有一个像这样的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>

如何从后面的代码(C#)更改绑定状态<Label>中的文本文本的颜色?

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

1 个答案:

答案 0 :(得分:0)

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

这:

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

成为这个:

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