gottsche
gottsche

Reputation: 83

MVVM binding is not updating UI element

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.

Upvotes: 0

Views: 1196

Answers (2)

user2153378
user2153378

Reputation:

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.

Upvotes: 2

JKennedy
JKennedy

Reputation: 18799

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

Upvotes: 1

Related Questions