Wpf 基于父样式属性的样式触发器
我想有小图标作为内容的按钮 图标定义并存储在Wpf 基于父样式属性的样式触发器,wpf,xaml,datatrigger,Wpf,Xaml,Datatrigger,我想有小图标作为内容的按钮 图标定义并存储在ResourceDictionary中,如下所示: <Path x:Key="BackIcon" Data="F1 M 57,42L 57,34L 32.25,34L 42.25,24L 31.75,24L 17... "/> <Path x:Key="LoadFromFileIcon" Data="F1 M 48,39L 56,39L 56,49L 63.25,49L 52,60.2... "/> <Path x:Key
ResourceDictionary
中,如下所示:
<Path x:Key="BackIcon" Data="F1 M 57,42L 57,34L 32.25,34L 42.25,24L 31.75,24L 17... "/>
<Path x:Key="LoadFromFileIcon" Data="F1 M 48,39L 56,39L 56,49L 63.25,49L 52,60.2... "/>
<Path x:Key="SaveToFileIcon" Data="F1 M 48,60L 56,60L 56,50L 63.25,50L 52,38.75L... "/>
<Button Style="{StaticResource IconButtonStyle}"
Content="{StaticResource ResetIcon}"/>
<Button Style="{DynamicResource IconButtonStyle}" IsEnabled="False" Content="{StaticResource _rect}">
</Button>
这就是我想到的:
<Style x:Key="IconButtonStyle" TargetType="Button">
<Style.Resources>
<Style TargetType="Path">
<Setter Property="Fill" Value="Black"/> <!-- Default path fill. -->
<Style.Triggers>
<!-- How can I set path fill to "Red" based on the parent Button IsEnabled property? -->
</Style.Triggers>
</Style>
</Style.Resources>
<Style.Triggers>
<Trigger Property="IsEnabled" Value="false">
<!-- ?? -->
</Trigger>
</Style.Triggers>
</Style>
我的自定义图标按钮具有通过style.Resources
定义的默认路径样式。设置默认路径填充很容易,但我不知道如何设置触发器,在禁用所有者按钮时将路径填充更改为红色
有可能吗?好吧。。。我想在继续之前你需要修改一些东西。 首先,在XAML中,您应该使用如下代码:
<Path x:Key="BackIcon" Data="F1 M 57,42L 57,34L 32.25,34L 42.25,24L 31.75,24L 17... "/>
<Path x:Key="LoadFromFileIcon" Data="F1 M 48,39L 56,39L 56,49L 63.25,49L 52,60.2... "/>
<Path x:Key="SaveToFileIcon" Data="F1 M 48,60L 56,60L 56,50L 63.25,50L 52,38.75L... "/>
<Button Style="{StaticResource IconButtonStyle}"
Content="{StaticResource ResetIcon}"/>
<Button Style="{DynamicResource IconButtonStyle}" IsEnabled="False" Content="{StaticResource _rect}">
</Button>
在本例中,我使用了在参考资料中定义的矩形。这是矩形的代码:
<Rectangle Width="10" Height="10" Style="{StaticResource ContentButtonPathStyle}" x:Key="_rect"></Rectangle>
显然,您将使用路径而不是矩形。。。
您注意到样式设置为ContentButtonPathStyle
这是该样式的代码:
<Style x:Key="ContentButtonPathStyle" TargetType="{x:Type Rectangle}">
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource=
{RelativeSource Mode=FindAncestor, AncestorType={x:Type Button}}, Path=IsEnabled}" Value="False">
<Setter Property="Fill" Value="Red"/>
</DataTrigger>
<DataTrigger Binding="{Binding RelativeSource=
{RelativeSource Mode=FindAncestor, AncestorType={x:Type Button}}, Path=IsEnabled}" Value="True">
<Setter Property="Fill" Value="Black"/>
</DataTrigger>
</Style.Triggers>
</Style>
请注意,必须在矩形(路径)之前定义ContentButtonPathStyle
最后一点是,您甚至不需要为按钮指定样式。除非你出于其他目的需要它。这太完美了。谢谢你的帮助。