Bind to Property in App.xaml.cs

2020-02-24 02:53发布

I have a Property in my App.xaml.cs called User that holds the User details. I have read here that you can't have a dependency property on the App class.

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

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

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

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

Thanks -Oliver

3条回答
贪生不怕死
2楼-- · 2020-02-24 03:15

And to access it programatically in C# you can do this:

((App)Application.Current).YourMethod
((App)Application.Current).YourProperty
查看更多
够拽才男人
3楼-- · 2020-02-24 03:22

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

See my blog post here

查看更多
Root(大扎)
4楼-- · 2020-02-24 03:26

I have read here that you can't have a dependency property on the App class.

Indeed you can't, because Application doesn't inherit from DependencyObject. However, it's not necessary : only the target property of a binding needs to be a dependency property.

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

IsEnabled="{Binding Path=User, Source={x:Static Application.Current}}"
查看更多
登录 后发表回答