C# wpf xaml 绑定到在后面的代码中创建的对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19981966/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 16:18:47  来源:igfitidea点击:

wpf xaml binding to object created in code behind

c#wpfxamlbinding

提问by xnonamex

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

刚开始,我对 C# 和 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.

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

In general my code looks something like this:

一般来说,我的代码看起来像这样:

MainWindow.xaml.cs

主窗口.xaml.cs

public partial class MainWindow : Window
{
    MyTestObject myTestObject;

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

    }
}

MyTestObject .cs

我的测试对象 .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));
    }
}

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.

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

采纳答案by Michael G

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

将数据上下文设置为 myTestObject。或者,为 myTestObject 创建一个公共属性并将您的 Xaml 绑定设置为 {Binding MyTestObjectPropertyHere.TestString}

For example:

例如:

public partial class MainWindow : Window
{
    MyTestObject myTestObject;

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

        this.DataContext = myTestObject;

        InitializeComponent();

    }
}

Xaml

xml

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

Example with binding to the MainWindow as the datacontext:

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

public partial class MainWindow : Window
{
    MyTestObject myTestObject;

    public MyTestObject MyTestObjectProperty { get { return myTestObject; } }

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

        this.DataContext = this;

        InitializeComponent();

    }
}

Xaml

xml

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