在ListViewItem中设置InputBinding(KeyboardBinding)
我希望设置 listViewitem 的输入绑定...它应该是键盘绑定而不是鼠标绑定...
我想当用户选择一个项目并按 Enter 时在我的视图模型中执行一个函数 的关键
样式
<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>
code> ListViewItem我的 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>
我似乎找不到一种方法来做到这一点...
我在 DataTemplate 中附加了我的 InputBinding 以及 Style Nothing Works
<KeyBinding Key="Enter" Command="{Binding Path=DataContext.KeyCommand, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Window}}}" CommandParameter="{Binding}"></KeyBinding>
I wish to set the Inputbinding of a listViewitem ... It should be a Keyborad binding and not a mouse binding...
I want to Execute a function in my view model when the User selectes an item and presses Enter
Key
Style for 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>
My 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>
I Cant Seem to find a way to do it...
I attached my InputBinding in DataTemplate as well has in Style Nothing Works
<KeyBinding Key="Enter" Command="{Binding Path=DataContext.KeyCommand, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Window}}}" CommandParameter="{Binding}"></KeyBinding>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否使用 4.0 之前的任何 .Net 版本?如果是这样,
KeyBinding.Command
和KeyBinding.CommandParameter
上的绑定将不起作用。为此,您必须使用 CommandReference API。否则,如果您使用 .Net 4.0,则将
KeyBinding
添加到ListView
的InputBindings
。KeyBinding.CommandParameter
绑定到ListView
的SelectedItem
。这样,命令将在按下
Enter
参数时执行,该参数将成为 ListView 的选定项目(这就是我认为您想要实现的目标)Are you using any .Net version prior to 4.0? If so the binding on
KeyBinding.Command
andKeyBinding.CommandParameter
wont work. You will have to useCommandReference
API for that.Otherwise if you are using .Net 4.0, then
KeyBinding
toListView
'sInputBindings
.KeyBinding.CommandParameter
to theSelectedItem
of theListView
.This way the command executes on
Enter
key press for the parameter which will be the selected item of the ListView (which is what I think you want to achieve)