Bind to a property in App.xaml.cs

I have a Property in my App.xaml.cs called User , which contains user information. I read here that you cannot have a dependency property in an App class.

I decided to use App.cs because it is global for the whole program, and this is used for access control, but any alternatives are welcome.

Now my question is: how can I bind this property to my UserControls and Windows.

 IsEnabled="{Binding Path=User, Converter={StaticResource hasAccessConverter}, ConverterParameter=Mid}" 

This obviously only works on a property on a DataContext . I want to access an application property. Can someone show me an example of binding to the App.xaml.cs property if I implement INotifyChanged ?

Thanks -Oliver

+7
data-binding wpf
source share
3 answers

I read here that you cannot have a dependency property in an App class.

In reality, you cannot, because Application does not inherit from DependencyObject . However, this is not necessary: ​​only the target property of the binding should be a dependency property.

If you want to bind to a property of your App class, you can do it like this:

 IsEnabled="{Binding Path=User, Source={x:Static Application.Current}}" 
+22
source share

And for its programming in C # you can do this:

 ((App)Application.Current).YourMethod ((App)Application.Current).YourProperty 
+4
source share

You can bind declaratively in Silverlight to Application.Current using a custom converter.

See my blog here

0
source share

All Articles