ListBox 项的输入绑定

Input binding for ListBox items

我有一个 ListBoxItemsSource 绑定到视图模型中的列表。 我想将命令绑定到每个项目的双击事件,并将项目作为参数。

示例:

<ListBox ItemsSource="{Binding Items}"/>

如果我只是将命令附加到 ListBox,我将无法将单击的 Item 作为参数发送。

示例:

<ListBox ItemsSource="{Binding Items}">
<ListBox.InputBinding>
    <MouseBinding Gesture="LeftDoubleClick" Command="MyCommand" CommandParameter="{ ?? No way to send the right item ?? }"/>
</ListBox.InputBinding> 

我也尝试将它绑定到 ItemTemplate 中的容器,如下所示:

<ListBox ItemsSource="{Binding Items}">
<ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.InputBindings>
                            <MouseBinding Gesture="LeftDoubleClick" Command="{Binding MyCommand}" CommandParameter="{Binding }"/>
                        </Grid.InputBindings>
                    <TextBlock Text="{Binding }"/>
                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate> 

它起作用了,但只有当您在 容器内单击时才会调用该命令。此处标记为黄色:

我希望在单击项目内的任意位置时调用该命令。 (选择项目时用蓝色标记的区域)。

我发现了一些类似的问题,但其中 none 的答案符合我的要求。

我该如何做到这一点?

添加此样式

<Style TargetType="ListBoxItem">
        <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
        <Setter Property="VerticalContentAlignment" Value="Stretch"/>
       <Setter Property="Padding" Value="0 0 0 0"/>
</Style>

默认ListBoxItem里面的ContentPresenter有Horizo​​ntalAlignment="Left"和VerticalAlignment="Center"。可以看下面ListBoxItem的Template

     <ControlTemplate TargetType="{x:Type ListBoxItem}">
                    <Border x:Name="Bd"
                            Padding="{TemplateBinding Padding}"
                            Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}"
                            SnapsToDevicePixels="True">
                        <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                          VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                          Content="{TemplateBinding Content}"
                                          ContentStringFormat="{TemplateBinding ContentStringFormat}"
                                          ContentTemplate="{TemplateBinding ContentTemplate}"
                                          SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                    </Border>
                    <ControlTemplate.Triggers>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsMouseOver" Value="True" />
                            </MultiTrigger.Conditions>
                            <Setter TargetName="Bd" Property="Background" Value="#1F26A0DA" />
                            <Setter TargetName="Bd" Property="BorderBrush" Value="#A826A0DA" />
                        </MultiTrigger>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="Selector.IsSelectionActive" Value="False" />
                                <Condition Property="IsSelected" Value="True" />
                            </MultiTrigger.Conditions>
                            <Setter TargetName="Bd" Property="Background" Value="#3DDADADA" />
                            <Setter TargetName="Bd" Property="BorderBrush" Value="#FFDADADA" />
                        </MultiTrigger>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="Selector.IsSelectionActive" Value="True" />
                                <Condition Property="IsSelected" Value="True" />
                            </MultiTrigger.Conditions>
                            <Setter TargetName="Bd" Property="Background" Value="#3D26A0DA" />
                            <Setter TargetName="Bd" Property="BorderBrush" Value="#FF26A0DA" />
                        </MultiTrigger>
                        <Trigger Property="IsEnabled" Value="False">
                            <Setter TargetName="Bd" Property="TextElement.Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>