IsDefault按钮单击不会更新最后一次触发

时间:2013-10-01 23:14:00

标签: c# wpf vb.net button focus

当通过IsDefault键盘执行按键代码输入键时,Textbox不会保存它的值(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> 将使用该修复程序,因为它没有代码隐藏功能。

感谢您的任何输入! 约什

0 个答案:

没有答案