user3567816
user3567816

Reputation: 156

Bind a TextBox to an object's property in WPF

I know you would say this question is duplicated, but I haven't found any working solution. So an easy scenario:

I have a textbox and a button:

<TextBox x:Name="textbox" Text="{Binding st.SomeText}">
<Button Click="Button_Click">Push</Button>

I have a class named Something:

public class Something
{
    public string SomeText { get; set; }
}

I have an object in the MainWindow class:

public partial class MainWindow : Window
{
    Something st;

    public MainWindow()
    {
        InitializeComponent();
        st = new Something();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        MessageBox.Show(st.SomeText);
    }
}

So I want to type something to the textbox, press the button and get a message contains the textbox's text, but I keep getting empty messages instead. What should I modify?

A lot of 'solution' said to use a Stackpanel or something and set the DataContext, but it didn't work either.

Upvotes: 2

Views: 21115

Answers (3)

Sachini Witharana
Sachini Witharana

Reputation: 116

Since you are using a button click on this scenario I believe a much simpler method would be for you to bind the text in the text box in the button_click method itself: cs file: public partial class MainWindow : Window {

    public MainWindow()
    {
        InitializeComponent();

    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        string myText;
        myText = txtBox.Text;
        MessageBox.Show(myText);
    }
}

XAML:

<TextBox Name="txtBox">

SO what happens here is Im getting the txt that i have typed on the textBox through the Name of the text box. And in the Button_Click method Im binding the text in the text box to a string and displaying the text through the Message.Show dialog box. Thanks Hope it helps helps. Cheers

Upvotes: 0

Joe
Joe

Reputation: 7004

How were you setting the data context?

The data context is where WPF looks for it's bindings. It seems you are trying to bind to your code-behind, in which case you need to set your DataContext to the object itself, and change the field st to a property.

private Something _st;
public Something st
{
    get
    {
        return _st;
    }
    set
    {
        _st = value;
        OnNotifyPropertyChanged("st");
    }
}

public MainWindow()
{
    InitializeComponent();
    this.DataContext = this;
    st = new Something();
}

However, if you're always wanting to bind to things inside st, it might make more sense to set the data context to the Something directly:

public MainWindow()
{
    InitializeComponent();
    st = new Something();
    this.DataContext = st ;
}

and simply bind like this:

<TextBox Text="{Binding SomeText}">

This means you don't need to implement INotifyPropertyChanged, or have st as a public property on your Window (But you should on Something, firing a change notification in the setter of SomeText).

Upvotes: 2

Ehsan Sajjad
Ehsan Sajjad

Reputation: 62498

You first have to set the DataContext property to the instance of your ViewModel which in this case is SomeThing class, so you need to set the context in the CodeBehind of the xaml form like:

public partial class MainWindow : Window
{

    public MainWindow()
    {
        InitializeComponent();
        Something st = new Something();
        this.DataContext = st;
    }

}

and then in your view i.e xaml file you can set the binding of the control that which particular property to bind for textbox like:

<TextBox x:Name="textbox" Text="{Binding SomeText}">

and you should be using Commands instead of using the Click event of Buttons, if you want proper MVVM pattern in your WPF application.

You might want to look at How to Implement MVVM in WPF

Upvotes: 5

Related Questions