WPF XAML绑定对象code创建背后 [英] wpf xaml binding to object created in code behind

查看:127
本文介绍了WPF XAML绑定对象code创建背后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

就出发了,我很新的C#和XAML。

Just to start off I am quite new to C# and xaml.

我都看过,检查教程,关于绑定,但大部分我所看到创建的XAML的对象。不过,我想创建在code中的对象,然后绑定到它的属性。此外,我将在code定义后面几个对象。总的来说,我要绑定到文本框。

I have watched, checked tutorials, about binding, but most of what I have seen create an object in the xaml. However I want to create the object in the code and then bind to it's properties. Furthermore I will have several objects defined in code later on. In general I want to bind to text boxes.

在一般我的code看起来是这样的:

In general my code looks something like this:


public partial class MainWindow : Window
    {
        MyTestObject myTestObject;

        public MainWindow()
        {
            myTestObject= new MyTestObject ();
            this.DataContext = this;
            InitializeComponent();

        }
    }


class MyTestObject : INotifyPropertyChanged
    {
        public MyTestObject ()
        {

        }

        private string testString = "Test";
        public string TestString
        {
            get { return testString; }
            set
            {
                if (value == testString) return;
                testString = value;
                this.OnPropertyChanged("TestString");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                this.PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
        }
}

最后,我将有相当多的中显示多个文本框属性(数字)的,因为软件的目的是作为对发送的测量数据的外部硬件组件的接口。我曾尝试多种方式如何绑定,但尚未成功。我将不胜感激一个例子,如何到previously提到的属性绑定到一个文本框。

Eventually i will have quite a lot of properties (numerical) that are displayed in several text boxes, because the software is intended as an interface to an external hardware component that sends measured data. I have tried several ways how to bind but have not succeeded yet. I would be most grateful for an example how to bind the previously mentioned property to a text box.

推荐答案

设置的DataContext到myTestObject。或者,使公共财产为myTestObject和设置您的XAML绑定到{结合MyTestObjectPropertyHere.TestString}

Set the Datacontext to myTestObject. Or, make a public property for myTestObject and set your Xaml binding to {Binding MyTestObjectPropertyHere.TestString}

例如:

public partial class MainWindow : Window
{
    MyTestObject myTestObject;

    public MainWindow()
    {
        myTestObject = new MyTestObject ();

        this.DataContext = myTestObject;

        InitializeComponent();

    }
}

的XAML

<TextBox Text="{Binding Path=TestString}" />

与作为在DataContext绑定到主窗口示例:

Example with binding to the MainWindow as the datacontext:

public partial class MainWindow : Window
{
    MyTestObject myTestObject;

    public MyTestObject MyTestObjectProperty { get { return myTestObject; } }

    public MainWindow()
    {
        myTestObject = new MyTestObject ();

        this.DataContext = this;

        InitializeComponent();

    }
}

的XAML

<TextBox Text="{Binding Path=MyTestObjectProperty.TestString}" />

这篇关于WPF XAML绑定对象code创建背后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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