2013-09-25 21 views
5

Bir Metinim var KeyUp Olay Tetikleyici WPF'de bir komutla kablolu. Komut parametresi olarak basılan gerçek anahtarı geçmem gerekiyor.Anahtarlama parametresi olarak geçiş WPF Komutu Bağlama Metin Kutusu

Bu komut gayet iyi çalışıyor, ancak bunu işleyen kodun basılan gerçek anahtarı bilmesi gerekiyor (bu bir enter tuşu veya sadece bir harf değil, hatırlıyorum, bu yüzden TextBox'tan alamıyorum). Metin).

Bunun nasıl yapılacağını anlayamıyorum. XAML:

xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" 

XAML:

<TextBox Height="23" Name="TextBoxSelectionSearch" Width="148" Tag="Enter Selection Name" Text="{Binding Path=SelectionEditorFilter.SelectionNameFilter,UpdateSourceTrigger=PropertyChanged}" > 
     <i:Interaction.Triggers> 
      <i:EventTrigger EventName="KeyUp"> 
      <i:InvokeCommandAction Command="{Binding SelectionEditorSelectionNameFilterKeyUpCommand}" /> 
      </i:EventTrigger> 
     </i:Interaction.Triggers> 
</TextBox> 
+0

MVVMLight'ı kullandım ve 'EventArgs' komutunu iletebilirim: http://stackoverflow.com/a/6205643/352101 – Bolu

cevap

7

O InvokeCommandAction ile mümkün olduğunu sanmıyorum ama hızlı bir şekilde kabaca bu bir gibi görünebilir kendi Behavior oluşturabilirsiniz:

public class KeyUpWithArgsBehavior : Behavior<UIElement> 
{ 
    public ICommand KeyUpCommand 
    { 
     get { return (ICommand)GetValue(KeyUpCommandProperty); } 
     set { SetValue(KeyUpCommandProperty, value); } 
    } 

    public static readonly DependencyProperty KeyUpCommandProperty = 
     DependencyProperty.Register("KeyUpCommand", typeof(ICommand), typeof(KeyUpWithArgsBehavior), new UIPropertyMetadata(null)); 


    protected override void OnAttached() 
    { 
     AssociatedObject.KeyUp += new KeyEventHandler(AssociatedObjectKeyUp); 
     base.OnAttached(); 
    } 

    protected override void OnDetaching() 
    { 
     AssociatedObject.KeyUp -= new KeyEventHandler(AssociatedObjectKeyUp); 
     base.OnDetaching(); 
    } 

    private void AssociatedObjectKeyUp(object sender, KeyEventArgs e) 
    { 
     if (KeyUpCommand != null) 
     { 
      KeyUpCommand.Execute(e.Key); 
     } 
    } 
} 

ve sonra onu TextBox'a ekleyin:

<TextBox Height="23" Name="TextBoxSelectionSearch" Width="148" Tag="Enter Selection Name" Text="{Binding Path=SelectionEditorFilter.SelectionNameFilter,UpdateSourceTrigger=PropertyChanged}" > 
    <i:Interaction.Behaviors> 
      <someNamespace:KeyUpWithArgsBehavior 
       KeyUpCommand="{Binding SelectionEditorSelectionNameFilterKeyUpCommand}" /> 
    </i:Interaction.Behaviors> 
</TextBox> 

Sadece bu komutu kullanarak Key komutunu parametre olarak almalısınız.

+0

Hey üzgünüm, artık tüm olayın args olmasını istiyorum (yalnızca tuşa basma değil) ama Cevabınız anahtar basın, tam olay args almak için nasıl herhangi bir ipucu için çalışıyor? – DermFrench

+0

Sadece KeyUpCommand.Execute (e) ile deneyeceğim; – DermFrench

+0

Evet, bu benim için çalıştı - çok teşekkürler (sadece .Execute (e) yerine .Execute (e.key) tam olay args verir. Superb! – DermFrench