我想在图标上进行数据触发,但我找不到如何做到这一点:(我尝试过这个代码,baut不起作用,任何人都能帮助pleaaase吗?
<Style x:Key="ConnectionIcon" TargetType="{x:Type Rectangle}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=ConnectionStatus}" Value="True">
<Setter Property="Resources">
<Setter.Value>
<SolidColorBrush Color="Green" />
</Setter.Value>
</Setter>
<Setter Property="Fill">
<Setter.Value>
<VisualBrush Stretch="Fill" Visual="{StaticResource appbar_disconnect}" />
</Setter.Value>
</Setter>
</DataTrigger>
<DataTrigger Binding="{Binding Path=ConnectionStatus}" Value="False">
<Setter Property="Resources">
<Setter.Value>
<SolidColorBrush Color="Red" />
</Setter.Value>
</Setter>
<Setter Property="Fill">
<Setter.Value>
<VisualBrush Stretch="Fill" Visual="{StaticResource appbar_connect}" />
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
这里有一个小技巧,可以使用OpacityMask
并简单地更改Rectangle
的Fill
属性。
<Style x:Key="ConnectionIcon"
TargetType="{x:Type Rectangle}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=ConnectionStatus}"
Value="True">
<Setter Property="Fill"
Value="Green" />
<Setter Property="OpacityMask">
<Setter.Value>
<VisualBrush Stretch="Fill"
Visual="{StaticResource appbar_disconnect}" />
</Setter.Value>
</Setter>
</DataTrigger>
<DataTrigger Binding="{Binding Path=ConnectionStatus}"
Value="False">
<Setter Property="Fill"
Value="Red" />
<Setter Property="OpacityMask">
<Setter.Value>
<VisualBrush Stretch="Fill"
Visual="{StaticResource appbar_connect}" />
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
希望这能有所帮助。