2014-09-27 48 views
1

我有一個文本框,併爲該文本框添加了一個keydown事件。一切工作正常,但我只注意到,當我按'Backspace'和'Delete'鍵時,綁定命令沒有被調用。TextBox KeyDown觸發器事件不適用於退格鍵和刪除鍵

我查看XAML文件: -

<TextBox x:Name="textBox" Width="500" Text="{Binding TextBoxText, UpdateSourceTrigger=PropertyChanged}"> 
<i:Interaction.Triggers>     
    <i:EventTrigger EventName="KeyDown"> 
     <cmd:EventToCommand Command="{BindingPath=TextBoxKeyDownEvent}" PassEventArgsToCommand="True" />     
    </i:EventTrigger>    
</i:Interaction.Triggers> 
</TextBox> 

我的視圖模型CS文件: -

//TextBox Key Down Event Handler 
    private DelegateCommand _textBoxKeyDownEvent; 
    public ICommand TextBoxKeyDownEvent 
    { 
     get 
     { 
      if (_textBoxKeyDownEvent == null) 
      { 
       _textBoxKeyDownEvent = new DelegateCommand(TextBoxKeyDownEventHandler); 
      } 
      return _textBoxKeyDownEvent; 
     } 
     set { } 
    } 

有人可以給我一些建議

+1

的'EventToCommand'是你需要挖成一個黑箱。 – 2014-09-27 09:21:33

+0

你可以使用KeyBinding - 但你的TextBoxText屬性更新我猜。爲什麼在文本更改時收到通知時需要捕獲密鑰? – 2014-09-27 09:37:41

+0

@kallocaion:謝謝你的回答。伯我的情況綁定textProperty將無法正常工作。因爲程序會動態改變文本框的內容,然後我不想調用我的eventHandler。我有其他事件與相同的組件相關聯,並且我無法切換到其他事件,如鍵控。 – ifti24 2014-09-27 09:57:15

回答

3

編輯: 你必須使用PreviewKeyDown它的工作原理。 KeyDown沒有在Space和Delete上被觸發。如果忽略MVVM並將KeyDown的處理程序置於代碼隱藏之中,則也將失敗


如何將Text-Property綁定到您viewmodel中的字符串?

我構建了一個快速,簡單的例子。從左側的文本框

結果

文本都只是填充到TextBlock右側。

enter image description here

查看

<Window x:Class="WpfApplication1.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="350" Width="525"> 
    <StackPanel Orientation="Horizontal"> 
     <TextBox Text="{Binding TextBoxValue, UpdateSourceTrigger=PropertyChanged}" Width="250"/> 
     <StackPanel Orientation="Horizontal"> 
      <TextBlock>"</TextBlock> 
      <TextBlock Text="{Binding TextBoxValue, UpdateSourceTrigger=PropertyChanged}" /> 
      <TextBlock>"</TextBlock> 
     </StackPanel> 
    </StackPanel> 
</Window> 

視圖模型

public class MainWindowViewModel : INotifyPropertyChanged 
{ 
    private string textBoxValue; 

    public string TextBoxValue 
    { 
     get { return textBoxValue; } 
     set 
     { 
      textBoxValue = value; 
      OnTextBoxValueChanged(); 
      RaisePropertyChanged(); 
     } 
    } 

    void OnTextBoxValueChanged() 
    { 
     // you logic here, if needed. 
    } 

    #region INotifyPropertyChanged implementation 

    public event PropertyChangedEventHandler PropertyChanged; 

    void RaisePropertyChanged([CallerMemberName] string propertyName = null) 
    { 
     if (PropertyChanged != null) 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
    } 

    #endregion 
} 
+0

感謝您的回答。伯我的情況綁定textProperty將無法正常工作。因爲程序會動態改變文本框的內容,然後我不想調用我的eventHandler。我有其他事件與相同的組件相關聯,並且我無法切換到其他事件,如鍵控。 – ifti24 2014-09-27 09:56:24

+0

感謝ec80r。你是對的。我必須使用PreviewKeyDown。它的工作現在。 – ifti24 2014-09-27 13:55:52

0

編輯:你是正確的 - 不執行默認行爲。您應該使用ec8ors解決方案,這是更好的呢:

<TextBox x:Name="textBox" Width="500" Text="{Binding TextBoxText, UpdateSourceTrigger=PropertyChanged}"> 
    <i:Interaction.Triggers> 
     <i:EventTrigger EventName="PreviewKeyDown"> 
      <i:InvokeCommandAction Command="{Binding TextBoxKeyDownEvent, Mode=OneWay}"/> 
     </i:EventTrigger> 
    </i:Interaction.Triggers> 
</TextBox> 

原始: 您可以使用化InputBindings打電話給你的命令時,「特殊」鍵按下:

<TextBox x:Name="textBox" Width="500" Text="{Binding TextBoxText, UpdateSourceTrigger=PropertyChanged}"> 
    <i:Interaction.Triggers>     
     <i:EventTrigger EventName="KeyDown"> 
      <cmd:EventToCommand Command="{BindingPath=TextBoxKeyDownEvent}" PassEventArgsToCommand="True" />     
     </i:EventTrigger>    
    </i:Interaction.Triggers> 

    <TextBox.InputBindings> 
     <KeyBinding Command="{Binding TextBoxKeyDownEvent}" Key="Delete" /> 
     <KeyBinding Command="{Binding TextBoxKeyDownEvent}" Key="Back" /> 
    </TextBox.InputBindings> 
</TextBox> 
+0

我試過你的方法。但在這種情況下,鍵綁定方法正在被調用和執行,但Delete和Back鍵會丟失它們的默認行爲。這意味着他們不會從光標位置刪除內容。 – ifti24 2014-09-27 11:05:00

0

你最使用PreviewKeyDown事件。

像這樣:

<EventSetter Event="PreviewKeyDown" Handler="TextBox_PreviewKeyDown"/> 
+0

我正在使用MVVM。 – ifti24 2014-09-27 11:24:30

相關問題