WPF数据绑定与代码边中的属性 [英] WPF Databinding with properties in codeside

查看:140
本文介绍了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.


这篇关于WPF数据绑定与代码边中的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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