Textbox binding update in WPF [duplicate]

左心房为你撑大大i 提交于 2020-05-07 09:55:19

问题


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");
    }
}

回答1:


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}"/>


来源:https://stackoverflow.com/questions/12019737/textbox-binding-update-in-wpf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!