app.xaml.cs 中的依赖属性
我是 WPF 新手,下面的问题对很多人来说可能看起来很愚蠢,请原谅我。
如何在 app.xaml.cs 中创建依赖属性?
事实上,我尝试创建它。下面的代码
public static DependencyProperty TempProperty =
DependencyProperty.Register("Temp", typeof(string), typeof(App));
public string Temp
{
get { return (string)GetValue(TempProperty); }
set { SetValue(TempProperty, value); }
}
抛出以下编译时错误:
当前上下文中不存在名称“GetValue”
当前上下文中不存在名称“SetValue”
有人可以帮助我吗?
谢谢你!
I am new to WPF and the below question may look silly for many, please pardon me.
How can I create a dependency property in app.xaml.cs?
Actually, I tried to created it. The below code,
public static DependencyProperty TempProperty =
DependencyProperty.Register("Temp", typeof(string), typeof(App));
public string Temp
{
get { return (string)GetValue(TempProperty); }
set { SetValue(TempProperty, value); }
}
throws the below compile time errors:
The name 'GetValue' does not exist in the current context
The name 'SetValue' does not exist in the current context
Can anybody help me in this?
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
DependencyProperties 只能在 DependencyObjects 上创建,并且由于 Application(您的 App 类继承自)没有实现它,因此您无法直接在 App 类上创建 DependencyProperty。
我假设您希望此属性支持绑定。如果是这种情况,您有两个选择:
即,如果您的新属性称为 Properties,您可以像这样绑定:
如果该属性需要成为 Binding 的目标,那么选项 #2 是您最好的选择。
DependencyProperties can only be created on DependencyObjects, and since Application (which your App class inherits from) doesn't implement it, you can't create a DependencyProperty directly on the App class.
I assume you want this property to support binding. If this is the case, you have two options:
i.e if your new property is called Properties, you can bind like so:
If the property needs to be the target of a Binding, then option #2 is your best bet.
包含依赖属性的类必须继承自 DependencyObject。
You class that contains dependency properties must inherit from DependencyObject.