WPF / c# - 从UI绑定到静态属性 [英] WPF / c# - Binding from UI to static property

查看:366
本文介绍了WPF / c# - 从UI绑定到静态属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难将我在TextBox中输入的文本绑定到非静态类中的静态属性。以下是具体细节:



UI位于主窗口中,文本框名称为 tb

该类(名为 c )是非静态和公共的。

属性(名为 p )是静态的 - 如 public static string p {get;设置;}

所需的方向是从UI到属性;另一个方向不是必需的,但没有区别。

我已经尝试了很多语法示例,但无法设法这样做。



请帮忙!

解决方案

我不确定我是否理解你(因为:所需的方向是从UI到物业;另一个方向不是必需的,但没有区别),但是......



我建议读这个: 静态类和静态类成员(C#编程指南) [ ^ ] - 静态成员部分:

MSDN写道:

非静态类可以包含静态方法,字段,属性或事件。即使没有创建类的实例,静态成员也可以在类上调用。 静态成员始终由类名访问,而不是实例名。





这也许有帮助:静态构造函数(C#编程指南) [ ^ ]



所以,你必须重新考虑你的类定义。


你试过x:static吗?

http://stackoverflow.com/questions/32395/accessing-static-fields-in-xaml [ ^ ]


< blockquote>你不能绑定到那样的静态。由于没有涉及的DependencyObject(或实现INotifyPropertyChanged的对象实例),绑定无法获得更新通知。


I am having a hard time binding the text I enter in a TextBox to a static property in a non-static class. Here are the specifics:

UI is in the main window, the textbox is names tb.
The class (named c) is non-static and public.
the property (named p) is static - as in public static string p { get; set;}
the required direction is from the UI to the property; The other direction is not required, but doesn't make a difference.
I have tried lots of examples of syntax, but cannot manage to do so.

Please help!

解决方案

Not sure i understand you well (because of this: "the required direction is from the UI to the property; The other direction is not required, but doesn't make a difference"), but...

I'd suggest to read this: Static Classes and Static Class Members (C# Programming Guide)[^] - Static Members part:

MSDN wrote:

A non-static class can contain static methods, fields, properties, or events. The static member is callable on a class even when no instance of the class has been created. The static member is always accessed by the class name, not the instance name.



This might be helpful too: Static Constructors (C# Programming Guide)[^]

So, you have to re-think your class definition.


Did you tried x:static?
See http://stackoverflow.com/questions/32395/accessing-static-fields-in-xaml[^]


You can't bind to a static like that. There's no way for the binding to get notified of updates since there's no DependencyObject (or object instance that implement INotifyPropertyChanged) involved.


这篇关于WPF / c# - 从UI绑定到静态属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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