wpf xaml 绑定到在后面的代码中创建的对象 [英] wpf xaml binding to object created in code behind

查看:27
本文介绍了wpf xaml 绑定到在后面的代码中创建的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

刚开始,我对 C# 和 xaml 还很陌生.

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

我看过、检查过关于绑定的教程,但我所看到的大部分内容都在 xaml 中创建了一个对象.但是我想在代码中创建对象,然后绑定到它的属性.此外,稍后我将在代码中定义几个对象.一般来说,我想绑定到文本框.

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.

通常我的代码看起来像这样:

In general my code looks something like this:

MainWindow.xaml.cs

public partial class MainWindow : Window
{
    MyTestObject myTestObject;

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

    }
}

MyTestObject .cs

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));
    }
}

最终我将拥有相当多的(数字)属性,它们将显示在多个文本框中,因为该软件旨在作为发送测量数据的外部硬件组件的接口.我尝试了几种绑定方式,但还没有成功.我将非常感谢如何将前面提到的属性绑定到 TextBox 的示例.

Eventually I will have quite a lot of (numerical) properties, which will be 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 of binding but I have not succeeded yet. I would be most grateful for an example how to bind the previously mentioned property to a TextBox.

推荐答案

将 Datacontext 设置为 myTestObject.或者,为 myTestObject 创建一个公共属性并将 Xaml 绑定设置为 {Binding 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}" />

绑定到 MainWindow 作为数据上下文的示例:

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 绑定到在后面的代码中创建的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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