在ListViewItem中设置InputBinding(KeyboardBinding)

发布于 2024-12-13 05:47:48 字数 4979 浏览 0 评论 0原文

我希望设置 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡淡的优雅 2024-12-20 05:47:48

您是否使用 4.0 之前的任何 .Net 版本?如果是这样,KeyBinding.CommandKeyBinding.CommandParameter 上的绑定将不起作用。为此,您必须使用 CommandReference API。

否则,如果您使用 .Net 4.0,则将

  1. KeyBinding 添加到 ListViewInputBindings
  2. 您必须将KeyBinding.CommandParameter 绑定到ListViewSelectedItem

这样,命令将在按下 Enter 参数时执行,该参数将成为 ListView 的选定项目(这就是我认为您想要实现的目标)

Are you using any .Net version prior to 4.0? If so the binding on KeyBinding.Command and KeyBinding.CommandParameter wont work. You will have to use CommandReference API for that.

Otherwise if you are using .Net 4.0, then

  1. Add a KeyBinding to ListView's InputBindings.
  2. You will have to bind KeyBinding.CommandParameter to the SelectedItem of the ListView.

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文