0

In my .xaml I have a textbox "textBox_Input" that is bound to a string

<Grid x:Name="LayoutRoot">
    <Button Content="{Binding Source={StaticResource LocStrings}, Path=ConnectDongle}" Command="{Binding ConnectDongleCommand}" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="155" Height="21"/>
    <Button Content="{Binding Source={StaticResource LocStrings}, Path=ReadDongleFile}" Command="{Binding ReadDongleFileCommand}" HorizontalAlignment="Left" Margin="11,36,0,0" VerticalAlignment="Top" Width="154"/>
    <Button Content="{Binding Source={StaticResource LocStrings}, Path=WriteDongleFile}" Command="{Binding WriteDongleFileCommand}" HorizontalAlignment="Left" Margin="10,63,0,0" VerticalAlignment="Top" Width="155"/>
    <TextBox x:Name="textBox_Input" Text="{Binding Path=TextBoxInputText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" Height="170" Margin="10,90,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="218"/>
    <TextBox Text="{Binding Path=TextBoxInputText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" Height="170" Margin="233,90,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="218"/>
</Grid>

The related ViewModel:

public RelayCommand ReadDongleFileCommand { private set; get; }
    public void ReadDongleFileCommandExecute()
    {
        TextBoxInputText += "a"; //this if just for testing
    }

    private String _textBoxInputText;
    public String TextBoxInputText
    {
        get
        {
            return _textBoxInputText;
        }
        set
        {
            _textBoxInputText = value;
        }
    }

When pressing the second Button bound to "ReadDongleFileCommand" both get and set of my TextBoxInputText are fired, so the command is working properly, but my TextBox is never updated. I never see any text in this TextBox.
I'm quite new to MVVM so probably I just forgot something simple, but I have no idea what it is.

Kurubaran
  • 8,696
  • 5
  • 43
  • 65
gottsche
  • 83
  • 4
  • 1
    Your view model should implement `INotifyPropertyChanged` interface and you need to fire `PropertyChanged` event in `TextBoxInputText` property setter – IVAAAN123 Sep 18 '14 at 07:21
  • Check [this](http://stackoverflow.com/questions/17764323/how-to-bind-a-simple-string-value-to-a-text-box/17764545#17764545) SO thread for how to bind a property to WPF textbox. – Kurubaran Sep 18 '14 at 07:24
  • Thank you, INotifyPropertyChanged was what I needed – gottsche Sep 18 '14 at 08:37

2 Answers2

2

You might want to take a look at the Binding.Elementname.

Maybe you need to watch this video (one of 3), it helped me out allot.
Here is also a really great (and entertaining) video, it takes a different approach.

1

You need to raise a property changed event on your TextBoxInputText..

Public class YourClass : INotifyPropertyChanged
{

    public YourClass()
    {
        ReadDongleFileCommand = new RelayCommand(ReadDongleFileCommandExecute);
    }

    public RelayCommand ReadDongleFileCommand { private set; get; }
    public void ReadDongleFileCommandExecute()
    {
        TextBoxInputText += "a"; //this if just for testing
    }

    private String _textBoxInputText;
    public String TextBoxInputText
    {
        get
        {
            return _textBoxInputText;
        }
        set
        {
            _textBoxInputText = value;
            OnPropertyChanged("TextBoxInputText");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged(string property)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(property));
    }
}

You probably want to look at the MVVM design pattern for more info found here

You will also need to hook your RelayCommand ReadDongleFileCommand up to the method ReadDongleFileCommandExecute() in the constructor of your class

JKennedy
  • 18,150
  • 17
  • 114
  • 198