Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding to a property in another namespace?

Tags:

c#

mvvm

wpf

xaml

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?

like image 360
Hardgraf Avatar asked Sep 08 '25 15:09

Hardgraf


1 Answers

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.

like image 87
outbred Avatar answered Sep 10 '25 05:09

outbred