如何从 Application.Resources 绑定到 ViewModel 属性?

How to bind to a ViewModel Property from Application.Resources?

我想绑定到 属性 ShowHat,它位于 ViewModel 中,名为“HatViewModel”,来自 Application.Resources

我可以通过这样的命名空间获得对 ViewModel 的访问权限:

xmlns:vm="clr-namespace:HatApp.ViewModels
<Application.Resources>
<vm:HatViewModel x:Key="HatVM"/>
</Application.Resources>

但是当我尝试在绑定中使用 ViewModel 的 属性 时,它不起作用。

<ControlTemplate.Triggers>
   <DataTrigger Binding="{Binding HatVM.ShowHat}" Value="True">
        <Setter Property="IsEnabled" Value="False"/>
    </DataTrigger>
</ControlTemplate.Triggers>

非常感谢任何帮助。

当您尝试从静态资源绑定 属性 时,您应该使用 Source

<DataTrigger Binding="{Binding ShowHat, Source={StaticResource HatVM}}" Value="True">