【问题标题】:Combobox won't select any item when clicking in margin between items单击项目之间的边距时,组合框不会选择任何项目
【发布时间】:2013-05-08 22:07:22
【问题描述】:

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

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

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

【问题讨论】:

    标签: wpf silverlight xaml layout combobox


    【解决方案1】:

    这几乎不是一个错误,如果你设置了一个边距,那么那个地方就没有项目那么为什么要选择一个呢?

    只需删除最外面的Grid 上的Margin 并将其包装在带有等效PaddingBorder 中,这会产生相同的结果,但现在应该可以点击(可能Border.Background 应该设置为Transparent 如果还没有命中测试)。

    【讨论】:

      【解决方案2】:

      我有同样的行为,我通过删除 ComboBoxItemContentPresenter 周围的边框上的填充来解决这个问题。我改为使用ContentPresenter 上的边距替换它。

      在未选择突出显示的 ComboBoxItem 的项目之间单击

      <ControlTemplate TargetType="ComboBoxItem">
        <Border Name="Border" SnapsToDevicePixels="true" CornerRadius="2" Padding="2">
          <ContentPresenter />
         </Border> 
         <!-- ...removed for brevity -->
      </ControlTemplate>
      

      修复:现在在项目之间单击选择突出显示的 ComboBoxItem

      <ControlTemplate TargetType="ComboBoxItem">
        <Border Name="Border" SnapsToDevicePixels="true" CornerRadius="2">
          <ContentPresenter Margin="2" />
         </Border> 
         <!-- ...removed for brevity -->
      </ControlTemplate>
      

      【讨论】:

        【解决方案3】:

        ComboBoxItem ControlTemplate 中的Grid 中删除Margin="1,1,1,1" 或从Grid 内部的ContentPresenter 中删除Margin="5,2,0,2" 有什么不同吗?

        我认为这必须是您的 ComboBoxItem ControlTemplate 上的边距/填充物。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-08-30
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多