WPF绑定到C#struct变量 [英] WPF bind to a C# struct variable
本文介绍了WPF绑定到C#struct变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,我知道一个类可以替换结构但是对于其他函数我需要能够使用结构。
我需要在下面做些什么才能使结构生效?我想绑定到我的struct中的变量。
注意:在手机上键入,请忽略首字母和小错误。
Hello, I'm aware that a class would work to replace the struct but for other functions I need to be able to use a struct.
What do I need to change below to make this work for a struct? I want to bind to a variable inside my struct.
Note : Typed out on phone so please ignore capitilsation and minor errors.
Public struct MyStruct : INotifyPropertyChanged
{
Public int _A;
Public int A {
Get { return _A; }
Set {
_A = value;
NotifyPropertyChanged("A");
}
}
Public event propertychangedeventhandler PropertyChanged;
Private void NotifyPropertyChanged(String info)
{
If(PropertyChanged != null){
propertychanged(this, new PropertyChangedeventargs(info));
}
}
};
Public class MyO{
Public testStruct names = new testStruct();
}
Public partial class Mainwindow : window{
MyO test = new MyO();
Public MainWindow(){
InitializeCommand();
Textbox1.DataContext = test.names._A;
Test.names2.A = 50;
}
}
推荐答案
该类的代码可能如下所示:
The code of the class could look like that:
public class MyClass : INotifyPropertyChanged
{
public int A {
get { return myStruct._A; }
set
{
myStruct._A = value;
NotifyPropertyChanged("A");
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string info)
{
if(PropertyChanged != null){
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
private MyStruct myStruct = new MyStruct();
}
根据结构的目的,您可能会决定公开它的最佳方式。这里假设结构不会直接改变。
Depending of the purpose of the structure, you might decide the best way to expose it. Here it is assumed that the structure will not be changed directly.
这篇关于WPF绑定到C#struct变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文