David Shnayder
David Shnayder

Reputation: 333

Data Binding To Property C# wpf

I need to bind a textbox to a property in the code behind, but I want it to be just to a property instead of class,

Instead of this:

public class A
{
    public string Text { get; set; }
}

textbox.DataContext = A
<textbox Text="{Binding Text}"/>

I want this:

public string Text { get; set; }
textbox.DataContext = Text;

How can I achieve this?

Upvotes: 0

Views: 46

Answers (1)

krivtom
krivtom

Reputation: 24916

I believe you should be able to achieve this by using

<textbox Text="{Binding}"/>

Alternative approach is to use the same form as data context, and not change the binding:

this.DataContext = this;

And in your XAML file leave the same binding:

<textbox Text="{Binding Text}"/>

In such case you would be binding to the property on your form class.

Upvotes: 1

Related Questions