样式触发不同的图像和边框背景

时间:2013-10-31 15:40:22

标签: wpf triggers styles

我想使用不同的边框背景和不同的图像(每个图像必须使用不同的背景颜色) 我如何改变使用 StyleTrigger 来做到这一点? (现在它只显示所有图像的红色边框)

<DataTemplate>
    <Border BorderThickness="1" Width="18" Height="18" CornerRadius="2" BorderBrush="Red" Background="Red">
        <Image Width="16" Height="16">
            <Image.Style>
                <Style TargetType="{x:Type Image}">
                    <Setter Property="Source" Value="{x:Null}" />
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding RowData.DataContext.my}" Value="High">
                                                    <Setter Property="Source" Value="/project;component/Images/High.png" />
                                                </DataTrigger>
                                                <DataTrigger Binding="{Binding RowData.DataContext.my}" Value="Medium">
                                                    <Setter Property="Source" Value="/project;component/Images/Medium.png" />
                                                </DataTrigger>
                                                <DataTrigger Binding="{Binding RowData.DataContext.my}" Value="Low">
                                                    <Setter Property="Source" Value="/project;component/Images/Low.png" />
                                                </DataTrigger>
                    </Style.Triggers>
                </Style>
            </Image.Style>
        </Image>
    </Border>
</DataTemplate>

谢谢。

1 个答案:

答案 0 :(得分:1)

假设您的Binding是正确的,您只需向Style添加类似的Border即可更新Background媒体资源,并移除Background来自Border本身的价值:

<DataTemplate>
    <Border BorderThickness="1" Width="18" Height="18" CornerRadius="2" BorderBrush="Red">
        <Border.Style>
            <Style TargetType="{x:Type Border}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding RowData.DataContext.my}" Value="High">
                        <Setter Property="Background" Value="Green" />
                    </DataTrigger>
                    <DataTrigger Binding="{Binding RowData.DataContext.my}" Value="Medium">
                        <Setter Property="Background" Value="Blue" />
                    </DataTrigger>
                    <DataTrigger Binding="{Binding RowData.DataContext.my}" Value="Low">
                        <Setter Property="Background" Value="Red" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Border.Style>
        <Image Width="16" Height="16">
            <Image.Style>
                <Style TargetType="{x:Type Image}">
                    <Setter Property="Source" Value="{x:Null}" />
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding RowData.DataContext.my}" Value="High">
                            <Setter Property="Source" Value="/project;component/Images/High.png" />
                        </DataTrigger>
                        <DataTrigger Binding="{Binding RowData.DataContext.my}" Value="Medium">
                            <Setter Property="Source" Value="/project;component/Images/Medium.png" />
                        </DataTrigger>
                        <DataTrigger Binding="{Binding RowData.DataContext.my}" Value="Low">
                            <Setter Property="Source" Value="/project;component/Images/Low.png" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </Image.Style>
        </Image>
    </Border>
</DataTemplate>

试试这个。