Textbox binding update in WPF

C#WpfXamlData Binding

C# Problem Overview


MessageText property gets updated only when I hit another control. What is more if I press any button it's Click handler isn't executed and the MessageText set is executed instead. I've broken my head.

<TextBox x:Name="messageText" Grid.Row="1" Grid.Column="0"
         TextWrapping="Wrap" Text="{Binding Path=MessageText, Mode=TwoWay}"/>

private void ChatView_Loaded(object sender, RoutedEventArgs e)
{
    DataContext = viewModel;
}

public string MessageText
{
    get
    {
        return this.messageText;
    }
    set
    {
        this.messageText = value;
        OnProperyChanged("MessageText");
    }
}

C# Solutions


Solution 1 - C#

You can adjust UpdateSourceTrigger to PropertyChanged

<TextBox x:Name="messageText" Grid.Row="1" Grid.Column="0"
                 TextWrapping="Wrap" Text="{Binding Path=MessageText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionOneMoreVladimirView Question on Stackoverflow
Solution 1 - C#Aghilas YakoubView Answer on Stackoverflow