2012-03-20 51 views
0

我有一個listbox如何處理頁面的相應視圖模型中的keydown事件以刪除listItems? 我在視圖中使用這個鱈魚:如何處理viewmodel中的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。

列表框

<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> 

您需要使用命令參考,如果你使用的是.NET 3.5在這個.net4.0工作正常

+0

謝謝@ adcool2007 – Tulsi 2012-03-20 12:02:19

0

我會考慮使用您的視圖模型的ICommand實例,它將從列表框中刪除項目。將KeyDown事件路由到命令是一件更棘手的事情。我建議看看MVVM LightEventToCommand行爲(如果你不想使用框架,你可以自己實現)。

或者,在你的View代碼中處理keydown事件,然後從那裏調用該命令。

相關問題