【问题标题】:How to bind to a ViewModel Property from Application.Resources?如何从 Application.Resources 绑定到 ViewModel 属性?
【发布时间】:2021-05-24 00:55:59
【问题描述】:

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

我可以通过这样的命名空间访问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>

非常感谢任何帮助。

【问题讨论】:

    标签: c# wpf viewmodel


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-25
      • 2011-02-01
      • 2011-08-26
      • 1970-01-01
      • 2015-11-24
      • 2012-07-20
      相关资源
      最近更新 更多