2012-03-20 10 views
0

私はリストボックスを持っていますどのように私はlistItemsを削除するページの対応するビューモデルでkeydownイベントを処理するのですか? 私はビューでこのタラを使用していますlistmodeのlistbox keydownイベントを処理してlistItemsを削除するにはどうすればいいですか?

<ListBox Margin="2,25,2,2" Grid.Row="3" ItemsSource="{Binding Path=CardViewModelSearchResults}" SelectedItem="{Binding Path=SelectedCategoryViewModel, Mode=TwoWay}" IsSynchronizedWithCurrentItem="True"> 
      <ItemsControl.ItemTemplate> 
       <DataTemplate > 
        <Grid> 
         <Grid.ColumnDefinitions> 
          <ColumnDefinition Width="*"/> 
          <ColumnDefinition /> 
          <ColumnDefinition /> 
         </Grid.ColumnDefinitions> 
         <TextBox Grid.Row="0" Grid.Column="0" ff:TextBoxBehaviour.TextChangedCommand="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl, AncestorLevel=1}, Path=DataContext.TextChanged}" Text="{Binding Path=CategoryName}" FontSize="14" FontWeight="Normal" BorderThickness="0" AllowDrop="False" /> 
         <TextBox Grid.Row="0" Grid.Column="1" Text="{Binding Path=CategoryID}" FontSize="14" FontWeight="Normal" Visibility="Hidden" /> 
        </Grid> 
       </DataTemplate> 
      </ItemsControl.ItemTemplate> 
     </ListBox> 

答えて

6

KeyBindingをアプリケーションに追加するか、特にListBoxに追加する必要があります。あなたは.net4.0で.NET 3.5を使用している場合は、これがadcool2007 @細かい

+0

おかげで働くコマンドリファレンスを使用する必要が

のListBox

<ListBox Margin="2,25,2,2" Grid.Row="3" ItemsSource="{BindingPath=CardViewModelSearchResults}" SelectedItem="{Binding Path=SelectedCategoryViewModel, Mode=TwoWay}" IsSynchronizedWithCurrentItem="True"> <ListBox.InputBindings> <KeyBinding Key="Delete" Command="{Binding Path=MyDeleteCommand}" /> </ListBox.InputBindings> </ListBox> 

Tulsi

0

を私はリストボックスから項目を削除しますあなたのViewModelでICommandインスタンスを使用して検討します。 KeyDownイベントをコマンドに送るのはもっと難しいことです。私はMVVM LightEventToCommandの動作を見てみることをお勧めします(フレームワークを使いたくない場合は、これを自分で実装できます)。

また、Viewのコードでkeydownイベントを処理し、そこからコマンドを呼び出すこともできます。

関連する問題