0

我想使用不同的边框背景和不同的图像(每个图像必须使用不同的背景颜色)我如何改变使用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>

谢谢你。

4

1 回答 1

1

假设你Binding是正确的,你只需要添加一个类似StyleBorder来更新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>

试试这个。

于 2013-10-31T15:45:12.460 回答