绑定到App.xaml.cs中的属性 [英] Bind to Property in App.xaml.cs

查看:171
本文介绍了绑定到App.xaml.cs中的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的App.xaml.cs中有一个属性,名为 User ,其中包含用户详细信息。
我已经阅读了这里,你不能有一个依赖属性在App类。

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.

我选择使用App.cs,因为它是整个程序的全局,这是用于访问控制,但欢迎任何替代品。

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.

现在我的问题是如何从我的UserControls和Windows绑定到此属性。

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

这显然只适用于 DataContext 上的一个属性。我想访问App上的属性。
如果我实现了 INotifyChanged

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?

感谢
-Oliver

Thanks -Oliver

推荐答案


我已经在这里看到你可以

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

这篇关于绑定到App.xaml.cs中的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆