如何为Wpf中的整行设置组合框项选择

如何为Wpf中的整行设置组合框项选择,wpf,combobox,selecteditem,Wpf,Combobox,Selecteditem,这是我的组合框数据模板 <ComboBox.ItemTemplate> <DataTemplate> <StackPanel KeyboardNavigation.DirectionalNavigation="Contained" Orientation="Horizontal"> <Image Source="{Binding Path=Flag}" He

这是我的组合框数据模板

<ComboBox.ItemTemplate>
            <DataTemplate>
                <StackPanel  KeyboardNavigation.DirectionalNavigation="Contained" Orientation="Horizontal">
                    <Image Source="{Binding Path=Flag}" Height="25"></Image>
                    <TextBlock Margin="10,0,0,0" Text="{Binding Path=Name}" VerticalAlignment="Center"></TextBlock>
                </StackPanel>
            </DataTemplate>
        </ComboBox.ItemTemplate>
        <ComboBox.ItemsPanel>
            <ItemsPanelTemplate>
                <VirtualizingStackPanel/>
            </ItemsPanelTemplate>
        </ComboBox.ItemsPanel>

它工作得很好,我想在鼠标位于行区域的任何位置时选择行,而不仅仅是当鼠标刚好位于数据上方时

谢谢

好的,由@Jehof解决,谢谢。第二个问题是“如果我设置了模板,为什么它不起作用?” 像这样

<Style TargetType="ComboBoxItem">
            <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
            <Setter Property="SnapsToDevicePixels" Value="true"/>
            <!--<Setter Property="OverridesDefaultStyle" Value="true"/>-->

            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ComboBoxItem">
                        <Border Name="Border" Padding="2" SnapsToDevicePixels="False" BorderThickness="1">
                            <ContentPresenter />
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsHighlighted" Value="true">
                                <Setter TargetName="Border" Property="Background" Value="{StaticResource BackgroundHighlighted}"/>
                                <Setter TargetName="Border" Property="BorderBrush" Value="{StaticResource BorderBrushHighlighted}"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

如果删除
。。。部分成功


@Jehof,如果我设置了模板,为什么它不起作用

    <Style TargetType="ComboBoxItem">
                <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                <Setter Property="SnapsToDevicePixels" Value="true"/>
                <!--<Setter Property="OverridesDefaultStyle" Value="true"/>-->

                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="ComboBoxItem">
                            <Border Name="Border" Padding="2" SnapsToDevicePixels="False" BorderThickness="1">
                                <ContentPresenter />
                            </Border>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsHighlighted" Value="true">
                                    <Setter TargetName="Border" Property="Background" Value="{StaticResource BackgroundHighlighted}"/>
                                    <Setter TargetName="Border" Property="BorderBrush" Value="{StaticResource BorderBrushHighlighted}"/>
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>

将ItemContainerStyle添加到组合框中,并将HorizontalContentAlignment设置为Stretch

<ComboBox.ItemContainerStyle>
  <Style TargetType="ComboBoxItem">
    <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
  </Style >
</ComboBox.ItemContainerStyle>

将ItemContainerStyle添加到组合框中,并将HorizontalContentAlignment设置为Stretch

<ComboBox.ItemContainerStyle>
  <Style TargetType="ComboBoxItem">
    <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
  </Style >
</ComboBox.ItemContainerStyle>