.net - 以编程方式强制 TextBox 值从 V 到 VM?

标签 .net wpf xaml data-binding mvvm

双向绑定(bind)效果很好,但在 TextBox 的情况下,它会在控件失去焦点时发生。

就我而言,我有一个简单的 UI,带有 1 个文本框和提交/取消按钮。

提交按钮在 View 上设置为默认值。因此,用户可以在文本框中输入文本并点击“Enter”,这将调用提交的命令。这很好,但我需要从 TextBox 访问值,它不在 VM 中,因为 TextBox 没有失去焦点。如何强制从我的代码更新此 binging?

<TextBox
    Text="{Binding Comment, Mode=TwoWay}"
    Grid.Row="2"/>

<StackPanel
    Grid.Row="3"
    HorizontalAlignment="Right"
    Orientation="Horizontal">
    <Button 
        Padding="20,5"
        Margin="5"
        Command="{Binding CancelCommand}"
        Content="Cancel" IsCancel="True" />
    <Button 
        Padding="20,5"
        Margin="5"
        Command="{Binding SubmitCommand}"
        Content="Submit" IsDefault="True" />
</StackPanel>

最佳答案

在 TextBox 绑定(bind)上使用 UpdateSourceTrigger=PropertyChanged。

关于.net - 以编程方式强制 TextBox 值从 V 到 VM?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12165471/

相关文章:

c# - MouseUp 事件在左键单击时不起作用

c# - 矩形不改变颜色WPF

wpf - 重新启动应用程序时 System.Windows.Markup.WpfXamlLoader.Load 中的 System.Threading.ThreadAbortException

c# - 如何将 DataContext 定义从代码隐藏移动到 XAML?

c# - 如何避免 System.Runtime.InteropServices.COMException?

.net - 进度条四舍五入到最接近的整数 - VB.NET

c# - Winforms MVP 网格事件问题

c# - Renci SSH.NET : can create symlink, 但删除符号链接(symbolic link)失败,为什么?

c# - 如何取消WPF窗体的最小化事件

c# - Windows 8 应用程序 : "Snapping" Drag and Drop for Card Game