1

当通过 IsDefault 键盘输入键执行按钮代码时,文本框不保存其值 (UpdateSourceTrigger)

我在代码隐藏中使用 .focus 进行修复,有更好的修复吗?

请不要建议 UpdateSourceTrigger=PropertyChanged 因为我不希望文本框代码在每次击键时都执行。

XAML

<Window x:Class="MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" DataContext="{Binding}">
<StackPanel Orientation="Vertical" Width="120">
<TextBox Text="{Binding ShowTextBox,UpdateSourceTrigger=Default}"  />
<Button Name="btnOK" Content="OK" IsDefault="True" Command="{Binding cmdOK}"  />
</StackPanel>

代码隐藏

Class MainWindow
Sub New()

    ' This call is required by the designer.
    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.
    Dim vmMain1 As New vmMainWindow
    Me.DataContext = vmMain1

End Sub

Private Sub btnOK_Click(sender As Object, e As RoutedEventArgs) Handles btnOK.Click
    ' btnOK.Focus()  'this line would be needed to get a fix that I am using now
End Sub
End Class

视图模型

Public Class vmMainWindow
Private _cmdOk As RelayCommand
Public ReadOnly Property cmdOK As RelayCommand
    Get
        If _cmdOk Is Nothing Then
            _cmdOk = New RelayCommand(Sub()
                                          MessageBox.Show(ShowTextBox)
                                      End Sub)
        End If
        Return _cmdOk
    End Get
End Property
Private _ShowTextBox As String = ""
Public Property ShowTextBox As String
    Get
        Return _ShowTextBox
    End Get
    Set(value As String)
        _ShowTextBox = value
    End Set
End Property
End Class

如果我取消注释 btnOK.Focus() 我会得到一个工作结果。此修复程序的一个问题是我不知道如何<Window.InputBindings><KeyBinding Key="A" Modifiers="Alt"/></Window.InputBindings> 使用该修复程序,因为它没有代码隐藏功能。

感谢您的任何意见!乔什

4

0 回答 0