2

这是我的组合框数据模板

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

如果删除 <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>
4

1 回答 1

4

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

<ComboBox.ItemContainerStyle>
  <Style TargetType="ComboBoxItem">
    <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
  </Style >
</ComboBox.ItemContainerStyle>
于 2013-07-03T08:42:22.107 回答