C# WPF根据内容将标签背景设置为图像

C# WPF根据内容将标签背景设置为图像,c#,wpf,xaml,data-binding,triggers,C#,Wpf,Xaml,Data Binding,Triggers,我有下面的Xaml代码和标签触发器。我想有一个触发器,它会在背景中放置一个图像,以获取一些内容值。作为一个触发器,我该怎么做 <Window.Resources> <DataTemplate x:Key="DataTemplate_Level2"> <Label Content="{Binding }" Width="70" Height="70" HorizontalContentAlignment="Center" x:Name="Ba

我有下面的Xaml代码和标签触发器。我想有一个触发器,它会在背景中放置一个图像,以获取一些内容值。作为一个触发器,我该怎么做

 <Window.Resources>
    <DataTemplate x:Key="DataTemplate_Level2">
        <Label Content="{Binding }" Width="70" Height="70" HorizontalContentAlignment="Center" x:Name="Background">
        </Label>
        <DataTemplate.Triggers>
            <DataTrigger Binding="{Binding}" Value="1">
                <Setter TargetName="Background" Property="Background" Value="Black"/>
            </DataTrigger>
            <DataTrigger Binding="{Binding}" Value="5">
                <Setter TargetName="Background" Property="Background" Value="Image"/>
            </DataTrigger>
            <DataTrigger Binding="{Binding }" Value="9">
                <Setter TargetName="Background" Property="Background" Value="Green"/>
            </DataTrigger>
            <DataTrigger Binding="{Binding}" Value="7">
                <Setter TargetName="Background" Property="Background" Value="blue"/>
            </DataTrigger>
        </DataTemplate.Triggers>
    </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>

只需使用ImageBrush作为背景

首先在资源中添加笔刷。 例如:


然后简单地使用StaticResource在特定触发器中设置它

    <DataTemplate x:Key="DataTemplate_Level2">
        <Label Content="{Binding }"
               Width="70"
               Height="70"
               HorizontalContentAlignment="Center"
               x:Name="Background">
        </Label>
        <DataTemplate.Triggers>
            <DataTrigger Binding="{Binding}"
                         Value="7">
                <Setter TargetName="Background"
                        Property="Background"
                        Value="{StaticResource MyImageBrush}" />
            </DataTrigger>
        </DataTemplate.Triggers>
    </DataTemplate>


是否要将
标签的
图像
设置为
背景
?是的,但仅当标签具有特定值时,例如,它需要像触发器一样
    <DataTemplate x:Key="DataTemplate_Level2">
        <Label Content="{Binding }"
               Width="70"
               Height="70"
               HorizontalContentAlignment="Center"
               x:Name="Background">
        </Label>
        <DataTemplate.Triggers>
            <DataTrigger Binding="{Binding}"
                         Value="7">
                <Setter TargetName="Background"
                        Property="Background"
                        Value="{StaticResource MyImageBrush}" />
            </DataTrigger>
        </DataTemplate.Triggers>
    </DataTemplate>