WPF数据绑定与代码边中的属性

问题描述:

嗨!我正在尝试读取反序列化为类的settingsfile并将其属性绑定并在UI中显示.

这是我正在做什么的简单演示.
我的testClass

Hi! Im trying read my settingsfile which is deserialized into class and bind its propertys and show it in my UI.

Here is simple demostration what im doing.
My testClass

public class TestClass
{
    private string property;

    public string Property
    {
        get { return property;  }
        set { property = value; }
    }
}


在我的应用程序中,我创建了TestClass,并在属性中插入了一些字符串.然后,执行对UI组件的绑定


In my application I have created TestClass and I insert some string into my property. Then I do following bindings to my UI component

//lets bind data     
Binding binding = new Binding("Property");
binding.Mode    = BindingMode.TwoWay;
binding.Source  = testClass.Property;
//Bind to Ui control
this.txtBox.SetBinding(TextBox.TextProperty,binding);


当我运行我的应用程序时,我的文本框中什么也没有发生.我没有在此演示中进行InotifypropertyChange事件,因为我在真正的问题中正在读取静态数据(设置文件).

希望你有我的主意!

干杯:)


When I run my application nothing happens in my textbox. I havent made InotifypropertyChange events in this demostration because im reading static data (settings file) in my real problem.

Hope you got my idea!

Cheers :)

将以下内容更改为

Change the following to

//lets bind data     
Binding binding = new Binding("Property");
binding.Mode    = BindingMode.TwoWay;
binding.Source  = testClass;
//Bind to Ui control
this.txtBox.SetBinding(TextBox.TextProperty,binding);







or

//lets bind data     
Binding binding = new Binding();
binding.Mode    = BindingMode.TwoWay;
binding.Source  = testClass.Property;
//Bind to Ui control
this.txtBox.SetBinding(TextBox.TextProperty,binding);



在Binding类构造函数上指定Path时,Source属性应该是对象.



When you specify Path on the Binding class contructor, the Source property should be the object.