ListBox项的输入绑定

时间:2018-02-22 15:03:27

标签: wpf listboxitem

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

示例:

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

它工作正常,但只有在点击容器内的 时才会调用命令。在这里标记为黄色:

enter image description here

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

我发现了一些类似的问题,但没有一个答案符合我的要求。

我如何做到这一点?

1 个答案:

答案 0 :(得分:1)

添加此款式

<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的模板

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