绑定到另一个命名空间中的属性? [英] Binding to a property in another namespace?
问题描述
我在主题 xaml 文件中有一个样式设置器.我正在尝试将 Setter 的值绑定到视图模型中的 bool peoperty.
I've got a style setter in a themes xaml file. I'm trying to bind the value of the Setter to a bool peoperty in a view model.
我在主题中获得了视图模型的命名空间:
I've got the namespace to the view model in themes:
xmlns:propertyGrid="clr-namespace:MY.App.Controls.PropertyGrid;assembly=MY.APP.Controls"
和样式中的绑定:
<Setter Property="IsExpanded" Value="{Binding Source={StaticResource propertyGrid:PropertyGridViewModel}, Path=AreCategoriesAutoExpanded}"/>
最后在视图模型中我只有一个 auto 属性:
Finally in the viewmodel I just have an auto property:
public bool AreCategoriesAutoExpanded { get; set; }
但是我在运行时遇到异常:
However I get an exception at run time:
Cannot find resource named 'propertyGrid:PropertyGridViewModel'. Resource names are case sensitive
如果我尝试使用动态资源资源,它会说明我只能绑定到 dp.这种绑定有什么问题?有什么我遗漏的吗?
If I try to use a dynamic resource resource it compains that I can only bind to a dp. What is wrong with this binding? Is there something I'm missing?
推荐答案
这仅在您的 ViewModel 是具有静态属性的静态类时才有效,如下所示:
This will only work if your ViewModel is a static class with a static property, like this:
<Setter Property="IsExpanded" Value="{Binding Source={x:Static propertyGrid:PropertyGridViewModel.AreCategoriesAutoExpanded}"/>
您错过了 'x:Static' 位,应该可以修复它.
You were missing the 'x:Static' bit, which should fix it.
这篇关于绑定到另一个命名空间中的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!