2011-07-20 150 views
2

我有一個TextBox,在我的窗口ListView,我想移動ListView的選擇上下而TextBox具有焦點:WPF:ApplicationCommands似乎忽略CommandTarget

enter image description here

但是,我似乎沒有得到我的CommandTarget聲明,他們被忽略。 MSDN says這是非RoutedCommands的默認行爲,但我嘗試使用的移動命令是RoutedUICommands,所以這可能不是此處的問題。

我錯過了什麼嗎?

我的XAML目前看起來像這樣(代碼後面是空的):

<Window x:Class="WpfTest.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="Test Window"> 
    <StackPanel> 
     <TextBox> 
      <TextBox.InputBindings> 
       <!-- for some reason, these two won't work --> 
       <KeyBinding Key="Down" 
          Command="ComponentCommands.MoveDown" 
          CommandTarget="{Binding ElementName=AllItemsList}"/> 
       <KeyBinding Key="Up" 
          Command="ComponentCommands.MoveUp" 
          CommandTarget="{Binding ElementName=AllItemsList}"/> 
      </TextBox.InputBindings> 
     </TextBox> 
    <ListView x:Name="AllItemsList"> 
      <ListViewItem>Item 1</ListViewItem> 
      <ListViewItem>Item 2</ListViewItem> 
      <ListViewItem>Item 3</ListViewItem> 
     </ListView> 
    </StackPanel> 
</Window> 

回答

1

其實自從RoutedUICommand從的RoutedCommand,得出兩者的支持命令目標(MSDN實際上說的命令目標上RoutedCommands唯一的工作,但它意味着它不適用於其他ICommand派生對象)。

你是否真的將組件命令(MoveDown和MoveUp)綁定到你的代碼後面的ListView中? ListView控件是空的命令綁定的第一次創建的時候,所以你需要做的是這樣的:

AllItemsList.CommandBindings.Add(new CommandBinding(ComponentCommands.MoveDown, ExecuteMoveDown)); 

那麼你將不得不寫你ExecuteMoveDown函數來進行移動。