0

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?

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

The first approach didn't work, it gave a run-time error... though the second approach did work so thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.