Binding to a property in another namespace?

梦想的初衷 提交于 2021-01-28 04:28:53

问题


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"

and the binding in the style:

<Setter Property="IsExpanded" Value="{Binding Source={StaticResource propertyGrid:PropertyGridViewModel}, Path=AreCategoriesAutoExpanded}"/>

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

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?


回答1:


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}"/>

You were missing the 'x:Static' bit, which should fix it.



来源:https://stackoverflow.com/questions/35346603/binding-to-a-property-in-another-namespace

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!