Wpf 在项目之间的空白处单击时,Combobox不会选择任何项目

Wpf 在项目之间的空白处单击时,Combobox不会选择任何项目,wpf,silverlight,xaml,layout,combobox,Wpf,Silverlight,Xaml,Layout,Combobox,这个问题最好用视频来解释。请看这里: 基本上,当鼠标位于选择矩形上时,我的组合框中的选择效果很好,但是每个项目的选择矩形之间有一个区域,下一个项目不会高亮显示,但是如果在鼠标位于该区域时单击,它的作用就好像您在组合框外单击一样-也就是说,它将恢复到旧的选择。我希望它选择当前突出显示的项目 我在谷歌上搜索过这个问题,认为这可能是一个WPF的一般问题,但什么也没找到,所以我认为这是我设计的风格。combobox确实使用了一些模板——我不确定什么是相关的,所以我将其中的大部分粘贴在下面。我尝试过在组合

这个问题最好用视频来解释。请看这里: 基本上,当鼠标位于选择矩形上时,我的组合框中的选择效果很好,但是每个项目的选择矩形之间有一个区域,下一个项目不会高亮显示,但是如果在鼠标位于该区域时单击,它的作用就好像您在组合框外单击一样-也就是说,它将恢复到旧的选择。我希望它选择当前突出显示的项目

我在谷歌上搜索过这个问题,认为这可能是一个WPF的一般问题,但什么也没找到,所以我认为这是我设计的风格。combobox确实使用了一些模板——我不确定什么是相关的,所以我将其中的大部分粘贴在下面。我尝试过在组合框项目矩形半径上乱搞一点,但没有成功。combobox的实际实例不会覆盖任何这些设置。任何帮助都将不胜感激。谢谢

<Style TargetType="{x:Type ComboBox}">
    <Setter Property="Foreground" Value="{DynamicResource TextBrush}" />
    <Setter Property="SnapsToDevicePixels" Value="true" />
    <Setter Property="Template" Value="{DynamicResource ComboBoxTemplate}" />
</Style>

<ControlTemplate x:Key="ComboBoxTemplate" TargetType="{x:Type ComboBox}">
    <Grid x:Name="grid">
        <ToggleButton Grid.Column="2" Template="{DynamicResource ComboBoxToggleButton}" x:Name="ToggleButton" Focusable="false" IsChecked="{Binding Path=IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" ClickMode="Press" >
            <TextBox Visibility="Hidden" Template="{DynamicResource ComboBoxTextBox}" HorizontalAlignment="Stretch" Margin="0,3" x:Name="PART_EditableTextBox" Style="{x:Null}" VerticalAlignment="Center" Focusable="True" Background="{DynamicResource LightBrush}" IsReadOnly="{TemplateBinding IsReadOnly}" BorderBrush="{DynamicResource NormalBorderBrush}" Foreground="{DynamicResource TextBrush}" />
        </ToggleButton>
        <ContentPresenter HorizontalAlignment="Left" Margin="3,3,23,3" x:Name="ContentSite" VerticalAlignment="Center" Content="{TemplateBinding SelectionBoxItem}" ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" IsHitTestVisible="False" />
        <Popup IsOpen="{TemplateBinding IsDropDownOpen}" Placement="Bottom" x:Name="Popup" Focusable="False" AllowsTransparency="True" PopupAnimation="Slide">
            <Grid MaxHeight="{TemplateBinding MaxDropDownHeight}" MinWidth="{TemplateBinding ActualWidth}" x:Name="DropDown" SnapsToDevicePixels="True">
                <Border x:Name="DropDownBorder" Background="{DynamicResource ControlBackgroundBrush}" CornerRadius="3,3,3,3" />
                <ScrollViewer Margin="4,6,4,6" Style="{DynamicResource NuclearScrollViewer}" SnapsToDevicePixels="True" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" CanContentScroll="True" Foreground="{DynamicResource {x:Static SystemColors.ActiveCaptionTextBrushKey}}">
                    <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained" />
                </ScrollViewer>
            </Grid>
        </Popup>
    </Grid>
    <ControlTemplate.Triggers>
        <Trigger Property="HasItems" Value="false">
            <Setter Property="MinHeight" Value="95" TargetName="DropDownBorder" />
        </Trigger>
        <Trigger Property="IsEnabled" Value="false">
            <Setter Property="Foreground" Value="{DynamicResource DisabledForegroundBrush}" />
            <Setter Property="Opacity" TargetName="grid" Value="0.5" />
        </Trigger>
        <Trigger Property="IsGrouping" Value="true">
            <Setter Property="ScrollViewer.CanContentScroll" Value="false" />
        </Trigger>
        <Trigger Property="AllowsTransparency" SourceName="Popup" Value="true">
            <Setter Property="Margin" Value="0,2,0,0" TargetName="DropDownBorder" />
        </Trigger>
        <Trigger Property="IsEditable" Value="true">
            <Setter Property="IsTabStop" Value="false" />
            <Setter Property="Visibility" Value="Visible" TargetName="PART_EditableTextBox" />
            <Setter Property="Visibility" Value="Hidden" TargetName="ContentSite" />
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

<Style d:IsControlPart="True" TargetType="{x:Type ComboBoxItem}">
    <Setter Property="Foreground" Value="{DynamicResource TextBrush}" />
    <Setter Property="SnapsToDevicePixels" Value="true" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ComboBoxItem}">
                <ControlTemplate.Resources>
                    <Storyboard x:Key="HoverOn">

                        <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="HoverRectangle" Storyboard.TargetProperty="(UIElement.Opacity)">
                            <SplineDoubleKeyFrame KeyTime="00:00:00.1000000" Value="1" />
                        </DoubleAnimationUsingKeyFrames>

                    </Storyboard>
                    <Storyboard x:Key="HoverOff">

                        <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="HoverRectangle" Storyboard.TargetProperty="(UIElement.Opacity)">
                            <SplineDoubleKeyFrame KeyTime="00:00:00.4000000" Value="0" />
                        </DoubleAnimationUsingKeyFrames>

                    </Storyboard>
                    <Storyboard x:Key="SelectedOn">

                        <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="SelectedRectangle" Storyboard.TargetProperty="(UIElement.Opacity)">
                            <SplineDoubleKeyFrame KeyTime="00:00:00.1000000" Value="1" />
                        </DoubleAnimationUsingKeyFrames>

                    </Storyboard>
                    <Storyboard x:Key="SelectedOff">

                        <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="SelectedRectangle" Storyboard.TargetProperty="(UIElement.Opacity)">
                            <SplineDoubleKeyFrame KeyTime="00:00:00.4000000" Value="0" />
                        </DoubleAnimationUsingKeyFrames>

                    </Storyboard>

                </ControlTemplate.Resources>
                <Grid SnapsToDevicePixels="true" Margin="1,1,1,1">
                    <Rectangle x:Name="Background" IsHitTestVisible="False" Opacity="0.25" Fill="{StaticResource NormalBrush}" RadiusX="1" RadiusY="1" />
                    <Rectangle x:Name="HoverRectangle" IsHitTestVisible="False" Opacity="0" Fill="{StaticResource NormalBrush}" RadiusX="1" RadiusY="1" />
                    <Rectangle x:Name="SelectedRectangle" IsHitTestVisible="False" Opacity="0" Fill="{StaticResource SelectedBackgroundBrush}" RadiusX="1" RadiusY="1" />
                    <ContentPresenter Margin="5,2,0,2" x:Name="contentPresenter" VerticalAlignment="Center" />
                    <Rectangle x:Name="FocusVisualElement" Visibility="Collapsed" Stroke="{StaticResource HoverShineBrush}" StrokeThickness="1" RadiusX="1" RadiusY="1" />
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsHighlighted" Value="true">
                        <Trigger.ExitActions>
                            <BeginStoryboard Storyboard="{StaticResource SelectedOff}" x:Name="SelectedOff_BeginStoryboard"/>
                        </Trigger.ExitActions>
                        <Trigger.EnterActions>
                            <BeginStoryboard Storyboard="{StaticResource SelectedOn}" x:Name="SelectedOn_BeginStoryboard"/>
                        </Trigger.EnterActions>

                    </Trigger>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Trigger.ExitActions>
                            <BeginStoryboard Storyboard="{StaticResource HoverOff}" x:Name="HoverOff_BeginStoryboard"/>
                        </Trigger.ExitActions>
                        <Trigger.EnterActions>
                            <BeginStoryboard Storyboard="{StaticResource HoverOn}"/>
                        </Trigger.EnterActions>
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter Property="Foreground" Value="{DynamicResource DisabledForegroundBrush}" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
从ComboBoxItem ControlTemplate中的网格中删除Margin=1,1,1,1,或者从该网格中的ContentPresenter中删除Margin=5,2,0,2,会有任何区别吗


我想这一定是ComboBoxItem控件模板上的边距/填充物。

这几乎不是一个bug,如果设置了边距,那么该位置没有项目,那么为什么要选择一个呢


只需删除最外层网格上的边距,并用同等填充物将其包裹在边框中,这会创建相同的结果,但现在应该可以单击边框。如果还没有命中测试,背景应该设置为透明。

我有相同的行为,我通过删除ComboBoxItem的ContentPresenter周围边框上的填充来修复。我用ContentPresenter上的边距替换了它

在未选择高亮显示的ComboBoxItem的项目之间单击 修复:在项目之间单击现在选择高亮显示的ComboBoxItem
<ControlTemplate TargetType="ComboBoxItem">
  <Border Name="Border" SnapsToDevicePixels="true" CornerRadius="2" Padding="2">
    <ContentPresenter />
   </Border> 
   <!-- ...removed for brevity -->
</ControlTemplate>
<ControlTemplate TargetType="ComboBoxItem">
  <Border Name="Border" SnapsToDevicePixels="true" CornerRadius="2">
    <ContentPresenter Margin="2" />
   </Border> 
   <!-- ...removed for brevity -->
</ControlTemplate>