Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
511 views
in Technique[技术] by (71.8m points)

c# - wpf xaml binding to object created in code behind

Just to start off I am quite new to C# and 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));
    }
}

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

For example:

public partial class MainWindow : Window
{
    MyTestObject myTestObject;

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

        this.DataContext = myTestObject;

        InitializeComponent();

    }
}

Xaml

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

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}" />

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...