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

时间:2013-07-03 08:39:11

标签: wpf combobox selecteditem

这是我的组合框datatemplate

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

它工作正常,我想在鼠标位于行区域的任何地方时选择行,而不仅仅是当它刚刚超过数据时。

感谢enter image description here

好吧,它由@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>

如果删除<Setter Property="Template"> ...部分可行!


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

1 个答案:

答案 0 :(得分:4)

将ItemContainerStyle添加到ComboBox,并将Horizo​​ntalContentAlignment设置为Stretch。

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