如何绑定到App Xaml后端中的属性 [英] How to Bind to Property Which Resides in the App Xaml backend
问题描述
我的App.xaml.cs中有一个属性
,名为 User
,其中包含用户详细信息。
我已阅读此处,您不能拥有依赖关系
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
上的属性。我想访问该应用程序上的属性。
如果我实现了 INotifyChanged
,有人可以给我展示绑定到App.xaml.cs属性的示例吗?
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
?
推荐答案
我在这里阅读到,您不能对App类具有依赖项属性。
I have read here that you can't have a dependency property on the App class.
的确可以,因为 Application
不继承自 DependencyObject
。但是,这不是必需的:仅绑定的 target 属性需要是依赖项属性。
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.
如果要绑定到属性App类,您可以这样操作:
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后端中的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!