问题
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