IsChecked 的触发器未触发
我想更改 ToggleButton 的图标(Fluent RibbonBar 的内容),具体取决于它的 IsChecked 属性。现在我编写了以下样式片段:
<Fluent:ToggleButton.Style>
<Style BasedOn="{StaticResource RibbonButtonStyle}" TargetType="{x:Type Fluent:ToggleButton}">
<Setter Property="OverridesDefaultStyle" Value="True"/>
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Fluent:ToggleButton}}, Path=IsChecked}" Value="False">
<Setter Property="Icon" Value="{StaticResource ResourceKey=Style.Images.Pined}"/>
</DataTrigger>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Fluent:ToggleButton}}, Path=IsChecked}" Value="True">
<Setter Property="Icon" Value="{StaticResource ResourceKey=Style.Images.Unpined}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Fluent:ToggleButton.Style>
问题是触发器不能很好地加载图像。问题不在于 IsChecked 本身没有实现,我已经对此进行了测试。而且我也没有在其他地方设置图标属性。如果我在其他地方使用图像资源,它们也可以正常工作。
重建信息:我已将 ToggleButton 放入 Backstage,作为放置在 BackstageTabItem 中的 RibbonListBox 的 DataTemplate。
I want to change the Icon of a ToggleButton (Content of Fluent RibbonBar) Depending on it's IsChecked Property. Now I have written the following style snippet:
<Fluent:ToggleButton.Style>
<Style BasedOn="{StaticResource RibbonButtonStyle}" TargetType="{x:Type Fluent:ToggleButton}">
<Setter Property="OverridesDefaultStyle" Value="True"/>
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Fluent:ToggleButton}}, Path=IsChecked}" Value="False">
<Setter Property="Icon" Value="{StaticResource ResourceKey=Style.Images.Pined}"/>
</DataTrigger>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Fluent:ToggleButton}}, Path=IsChecked}" Value="True">
<Setter Property="Icon" Value="{StaticResource ResourceKey=Style.Images.Unpined}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Fluent:ToggleButton.Style>
The problem is that the Trigger doesn't load the Image well. The problem is not, that IsChecked doesn't actualize itself, I've already tested this. And also I don't set the icon property anywhere else. The image resources also works fine if I use them otherwhere.
Information for Rebuild: I've put the ToggleButton into the Backstage as the DataTemplate of a RibbonListBox placed in a BackstageTabItem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于这是切换按钮的样式,因此您不需要 findancestor 绑定 - 您可以使用 self.实际上,由于
IsChecked
是一个 DP,您只需使用触发器 - 例如Since this is a style for togglebutton you don't need a findancestor binding - you would use self. And actually since
IsChecked
is a DP you can just use a trigger - eg<Trigger Property="IsChecked" Value="False">
您绝对需要从控件声明中删除
Icon
属性,因为它将覆盖样式尝试执行的所有操作,因为优先级。但是这些
DataTriggers
将不起作用,绑定将寻找一个祖先,本身被排除,因此您必须更改它们以及AndrewS已经指出的那样。只有满足这两个条件,您才有机会使其发挥作用(尽管甚至可能存在其他干扰)。You absolutely need to remove the
Icon
property from the control declaration as it will override everything a style tries to do due to precedence.But those
DataTriggers
will not work, the binding will look for an ancestor, itself excluded, so you must change them as well as already pointed out by AndrewS. Only if both conditions are met you have a chance of getting this to work (there may be even additional interferences though).