【问题标题】:Bind Path in ResourceDictionary's dependant on VM propertyResourceDictionary 中的绑定路径依赖于 VM 属性
【发布时间】:2015-06-03 05:23:04
【问题描述】:
我创建了一个 ResourceDictionary,它定义了一组在按钮 ControlTemplate 的 ContentPresenter 中使用的 System.Windows.Shapes.Path。
我想根据 ViewModel 属性更改其中一个路径。如果true 按钮使用ResourceDictionary 中的一个路径,如果false 使用不同的路径。
目前我只是在 xaml 中引用一个 StaticResource 来直接指向我想要显示的路径。
最好的方法是什么?
【问题讨论】:
标签:
c#
wpf
mvvm-light
resourcedictionary
【解决方案1】:
您必须通过引用资源字典中的元素来修改样式中按钮的内容模板。
类似这样的:
<Button.Style>
<Style TargetType="Button">
<Setter Property="ContentTemplate" Value="{StaticResource cp2}"></Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="ContentTemplate" Value="{StaticResource cp1}"></Setter>
</Trigger>
</Style.Triggers>
</Style>
</Button.Style>
我已使用鼠标悬停属性作为更改内容模板的触发器。
你可以用DataTrigger代替Trigger