2011-11-03 58 views
0

我想設置的ListViewItem的Inputbinding ......這應該是一個Keyborad約束力,而不是鼠標結合...設置InputBinding(KeyboardBinding)在ListViewItem的

我想在我的視圖模型來執行一個功能當用戶selectes一個項目,並按下Enter重點

風格的ListViewItem

<Style x:Key="{ComponentResourceKey TypeInTargetAssembly={x:Type CustomView:PlainView}, 
            ResourceId=ImageViewItem}"   
     TargetType="{x:Type ListViewItem}" BasedOn="{StaticResource {x:Type ListViewItem}}"> 

    <Setter Property="HorizontalContentAlignment" Value="Center" /> 
    <Setter Property="FocusVisualStyle" Value="{StaticResource ListViewItemFocusVisual}" /> 
    <Setter Property="BorderBrush" Value="Transparent" /> 
    <Setter Property="BorderThickness" Value="1" /> 
    <Setter Property="Margin" Value="0,0,0,1" /> 
    <Setter Property="Padding" Value="5,2,5,2" /> 
    <Setter Property="VerticalContentAlignment" Value="Center" /> 
    <Setter Property="Focusable" Value="False"></Setter> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type ListViewItem}"> 
       <Border x:Name="border" 
         BorderBrush="{TemplateBinding BorderBrush}" 
         BorderThickness="{TemplateBinding BorderThickness}" 
         CornerRadius="2" 
         SnapsToDevicePixels="true"> 
        <Grid Margin="2,0,2,0"> 
         <Rectangle x:Name="BackgroundGradientOver" 
            Fill="{DynamicResource MouseOverBrush}" 
            Opacity="0" 
            RadiusX="1" 
            RadiusY="1" 
            Stroke="{DynamicResource MouseOverBorderBrush}" /> 
         <Rectangle x:Name="BackgroundGradientSelectedDisabled" 
            Fill="{DynamicResource ListItemSelectedBrush}" 
            Opacity="0" 
            RadiusX="1" 
            RadiusY="1" 
            Stroke="{DynamicResource ListItemSelectedBorderBrush}" /> 
         <Rectangle x:Name="BackgroundGradientSelected" 
            Fill="{DynamicResource PressedBrush}" 
            Opacity="0" 
            RadiusX="1" 
            RadiusY="1" 
            Stroke="{DynamicResource PressedBorderBrush}" 
            StrokeThickness="1" /> 
         <ContentPresenter x:Name="contentPresenter" 
             Margin="{TemplateBinding Padding}" 
             HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
             Content="{TemplateBinding Content}" 
             ContentTemplate="{TemplateBinding ContentTemplate}" /> 
        </Grid> 
       </Border> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
    <Setter Property="Foreground" Value="{DynamicResource OutsideFontColor}" /> 
</Style> 

我的DataTemplate

<DataTemplate x:Key="centralTile"> 

    <StackPanel Width="80" 
       Height="40" 
       KeyboardNavigation.AcceptsReturn="True"> 
     <StackPanel.InputBindings> 
      <KeyBinding Key="Enter" Command="{Binding Path=DataContext.KeyCommand, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Window}}}" CommandParameter="{Binding}"></KeyBinding> 
     </StackPanel.InputBindings> 
     <Grid>    
      <Grid.ColumnDefinitions> 
       <ColumnDefinition Width="30" /> 
       <ColumnDefinition Width="*" /> 
      </Grid.ColumnDefinitions> 
      <Button Command="{Binding Path=DataContext.KeyCommand, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Window}}}" 
          CommandParameter="{Binding}"> 
       <TextBlock Text="{Binding Path=Name}" /> 
      </Button> 
      <Image Grid.Column="1" Source="Water lilies.jpg" /> 
     </Grid> 
     <TextBlock HorizontalAlignment="Center" 
        FontSize="13" 
        Text="{Binding Path=Name}" /> 
    </StackPanel> 
</DataTemplate> 

我似乎無法找到一個方法來做到這一點...

我重視我的InputBinding在DataTemplate中,以及在樣式沒有

  <KeyBinding Key="Enter" Command="{Binding Path=DataContext.KeyCommand, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Window}}}" CommandParameter="{Binding}"></KeyBinding> 

回答

2

你使用任何.NET版本4.0之前的作品?如果是這樣,那麼在KeyBinding.CommandKeyBinding.CommandParameter上的綁定將不起作用。你將不得不使用CommandReference API。

否則,如果您使用的是.NET 4.0,然後

  1. 添加KeyBindingListViewInputBindings
  2. 您必須將KeyBinding.CommandParameter綁定到ListViewSelectedItem

這樣的命令上Enter按鍵執行的,這將是ListView中的所選項目的參數(這就是我想你想達到的)

+0

我使用.NET 4.0客戶端配置文件。 ...我甚至試圖使用Net 4.0的KeyBinding.Command作品,如果我在選擇ListViewitem後按下「Tab」。 – Ankesh

+0

我想,我必須將輸入綁定在ListView ... – Ankesh

+0

但什麼情況下,我必須爲ListView和ListViewItem設置輸入綁定使用相同的鍵來爲ListView執行不同的功能& ListViewItem' .... – Ankesh