c# - WPF 使用 DataBinding 根据其值设置标签背景

标签 c# wpf xaml data-binding

我目前正在制作一个网格,每个单元格中都有一个标签,标签中的内容来自使用数据绑定(bind)的列表。 我正在尝试根据标签中的值更改每个单元格的颜色。例如,如果值 = 1,则背景必须为黑色。 这是我现在的代码:

  <Window.Resources>
    <DataTemplate x:Key="DataTemplate_Level2">
        <Label Content="{Binding}"  Width="70" Height="70" HorizontalContentAlignment="Center">
        </Label>
    </DataTemplate>

    <DataTemplate x:Key="DataTemplate_Level1">
        <ItemsControl ItemsSource="{Binding}" ItemTemplate="{DynamicResource DataTemplate_Level2}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Horizontal"/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
        </ItemsControl>
    </DataTemplate>
</Window.Resources>

我尝试了使用触发器的不同方式,但似乎没有任何效果。

如有任何帮助,我们将不胜感激。

谢谢

最佳答案

就这么简单,但是您将复制和粘贴大量 Setter 标签。您可能需要考虑使用值转换器(见下文)。

<DataTemplate x:Key="DataTemplate_Level2">
    <Grid 
        SnapsToDevicePixels="True" 
        x:Name="Background">
        <Label Content="{Binding}" />
    </Grid>
    <DataTemplate.Triggers>
        <DataTrigger Binding="{Binding}" Value="1">
            <Setter TargetName="Background" Property="Background" Value="Black" />
        </DataTrigger>
        <DataTrigger Binding="{Binding}" Value="2">
            <Setter TargetName="Background" Property="Background" Value="Khaki" />
        </DataTrigger>
        <DataTrigger Binding="{Binding}" Value="3">
            <Setter TargetName="Background" Property="Background" Value="YellowGreen" />
        </DataTrigger>
    </DataTemplate.Triggers>
</DataTemplate>

这是值转换器版本:

<Window.Resources>
    <local:ColorConverter x:Key="ColorConverter" />

    <DataTemplate x:Key="DataTemplate_Level2">
        <Grid 
            SnapsToDevicePixels="True" 
            Background="{Binding Converter={StaticResource ColorConverter}}">
            <Label Content="{Binding}" />
        </Grid>
    </DataTemplate>
</Window.Resources>

C#

public class ColorConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        Color clr = Colors.SteelBlue;

        var s = value as String;

        //  Add code here to pick a color or generate RGB values for one
        switch (s) {
            case "1":
                clr = Colors.Black;
                break;
        }

        return new SolidColorBrush(clr); 
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

关于c# - WPF 使用 DataBinding 根据其值设置标签背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36822910/

相关文章:

c# - 如何使用 WPF 中的 Frame 控件制作过渡效果?

c# - 我应该使用什么模式将 .NET UI 耦合到将提供回调更新的 .NET 库类

WPF 如何在屏幕旋转时禁用某些元素的旋转?

.net - 使用多个模板的 WPF 控件创作

WPF 架构困惑 RE : Routed Commands, 事件和手势

c# - 为什么这个简单的Mobile Form在使用播放器时没有关闭

c# - 在文本框中插入整数值

c# - 来自 C# 背景的哪个更容易学习 : Objective C or Java?

c# - 到达数据网格控件模板内的用户控件

c# - 如何异步加载和显示图像