1

我希望从组合框的 contentPresenter 的偏移量直接打开组合框的下拉列表(你知道......显示所选项目的部分)。但实际上发生的是下拉列表的偏移量高于 contentPresenter 直到页面的天花板。

我还希望下拉列表的宽度与最大的项目一致 - 永久,而不是像现在这样动态更改(现在,它根据视图中的项目调整大小)。

在此处输入图像描述

我尝试将组合框的 MaxDropDownHeight 属性设置为 0 或 300,但没有任何改变。

这是模板控件(图像与此片段无关):

<Style TargetType="ComboBox" >
        <Setter Property="Padding" Value="8,0" />
        <Setter Property="FlowDirection" Value="LeftToRight" />
        <Setter Property="Foreground" Value="{StaticResource ComboBoxForegroundThemeBrush}" />
        <Setter Property="Background" Value="White" />
        <Setter Property="BorderBrush" Value="Gray" />
        <Setter Property="BorderThickness" Value="1" />
        <Setter Property="TabNavigation" Value="Once" />
        <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled" />
        <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto" />
        <Setter Property="ScrollViewer.HorizontalScrollMode" Value="Disabled" />
        <Setter Property="ScrollViewer.VerticalScrollMode" Value="Auto" />
        <Setter Property="ScrollViewer.IsVerticalRailEnabled" Value="True" />
        <Setter Property="ScrollViewer.IsDeferredScrollingEnabled" Value="False" />
        <Setter Property="ScrollViewer.BringIntoViewOnFocusChange" Value="True" />
        <Setter Property="HorizontalContentAlignment" Value="Stretch" />
        <Setter Property="FontFamily" Value="{StaticResource ContentControlThemeFontFamily}" />
        <Setter Property="FontSize" Value="{StaticResource ControlContentThemeFontSize}" />
        <Setter Property="ItemsPanel">
            <Setter.Value>
                <ItemsPanelTemplate>
                    <CarouselPanel />
                </ItemsPanelTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ComboBox">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="32" />
                        </Grid.ColumnDefinitions>
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Normal" />
                                <VisualState x:Name="PointerOver">
                                    <!--
                                        <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="Background">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource ComboBoxPointerOverBackgroundThemeBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="Background">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource ComboBoxPointerOverBorderThemeBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Fill" Storyboard.TargetName="Highlight">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource ComboBoxSelectedPointerOverBackgroundThemeBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    -->
                                </VisualState>
                                <VisualState x:Name="Pressed">
                                    <!--
                                        <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="Background">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource ComboBoxPressedBackgroundThemeBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="Background">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource ComboBoxPressedBorderThemeBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource ComboBoxPressedForegroundThemeBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="PressedBackground"/>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="DropDownGlyph">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource ComboBoxArrowPressedForegroundThemeBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    -->
                                </VisualState>
                                <VisualState x:Name="Disabled">
                                    <!--
                                        <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="Background">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource ComboBoxDisabledBackgroundThemeBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="Background">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource ComboBoxDisabledBorderThemeBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource ComboBoxDisabledForegroundThemeBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="DropDownGlyph">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource ComboBoxArrowDisabledForegroundThemeBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    -->
                                </VisualState>
                            </VisualStateGroup>
                            <VisualStateGroup x:Name="FocusStates">
                                <VisualState x:Name="Focused">
                                    <!--
                                        <Storyboard>
                                        <DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="HighlightBackground"/>
                                        <DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="Highlight"/>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource ComboBoxFocusedForegroundThemeBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    -->
                                </VisualState>
                                <VisualState x:Name="FocusedPressed">
                                    <!--
                                        <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource ComboBoxPressedForegroundThemeBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Fill" Storyboard.TargetName="Highlight">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource ComboBoxPressedHighlightThemeBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    -->
                                </VisualState>
                                <VisualState x:Name="Unfocused" />
                                <VisualState x:Name="PointerFocused" />
                                <VisualState x:Name="FocusedDropDown">
                                    <!--
                                        <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Duration="0" Storyboard.TargetProperty="Visibility" Storyboard.TargetName="PopupBorder">
                                        <DiscreteObjectKeyFrame KeyTime="0">
                                        <DiscreteObjectKeyFrame.Value>
                                        <Visibility>Visible</Visibility>
                                        </DiscreteObjectKeyFrame.Value>
                                        </DiscreteObjectKeyFrame>
                                        </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    -->
                                </VisualState>
                            </VisualStateGroup>
                            <VisualStateGroup x:Name="DropDownStates">
                                <VisualState x:Name="Opened">
                                    <!--
                                        <Storyboard>
                                        <SplitOpenThemeAnimation ClosedTargetName="ContentPresenter" ContentTranslationOffset="0" ContentTargetName="ScrollViewer" ClosedLength="{Binding TemplateSettings.DropDownClosedHeight, RelativeSource={RelativeSource Mode=TemplatedParent}}" OffsetFromCenter="{Binding TemplateSettings.DropDownOffset, RelativeSource={RelativeSource Mode=TemplatedParent}}" OpenedTargetName="PopupBorder" OpenedLength="{Binding TemplateSettings.DropDownOpenedHeight, RelativeSource={RelativeSource Mode=TemplatedParent}}"/>
                                        </Storyboard>
                                    -->
                                </VisualState>
                                <VisualState x:Name="Closed">
                                    <!--
                                        <Storyboard>
                                        <SplitCloseThemeAnimation ClosedTargetName="ContentPresenter" ContentTranslationOffset="40" ContentTranslationDirection="{Binding TemplateSettings.SelectedItemDirection, RelativeSource={RelativeSource Mode=TemplatedParent}}" ContentTargetName="ScrollViewer" ClosedLength="{Binding TemplateSettings.DropDownClosedHeight, RelativeSource={RelativeSource Mode=TemplatedParent}}" OffsetFromCenter="{Binding TemplateSettings.DropDownOffset, RelativeSource={RelativeSource Mode=TemplatedParent}}" OpenedTargetName="PopupBorder" OpenedLength="{Binding TemplateSettings.DropDownOpenedHeight, RelativeSource={RelativeSource Mode=TemplatedParent}}"/>
                                        </Storyboard>
                                    -->
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <Border x:Name="Background"
                                Grid.ColumnSpan="2"
                                Background="{TemplateBinding Background}"
                                BorderBrush="{TemplateBinding BorderBrush}"
                                BorderThickness="{TemplateBinding BorderThickness}"
                                CornerRadius="2" />
                        <Rectangle x:Name="PressedBackground"
                                   Margin="{TemplateBinding BorderThickness}"
                                   Fill="{StaticResource ComboBoxPressedHighlightThemeBrush}"
                                   Opacity="0" />
                        <Border x:Name="HighlightBackground"
                                Grid.ColumnSpan="2"
                                Background="{StaticResource ComboBoxFocusedBackgroundThemeBrush}"
                                BorderBrush="{StaticResource ComboBoxFocusedBorderThemeBrush}"
                                BorderThickness="{TemplateBinding BorderThickness}"
                                Opacity="0" />
                        <Rectangle x:Name="Highlight"
                                   Margin="{TemplateBinding BorderThickness}"
                                   Fill="{StaticResource ComboBoxSelectedBackgroundThemeBrush}"
                                   Opacity="0" />
                        <ContentPresenter x:Name="ContentPresenter"
                                          Margin="{TemplateBinding Padding}"
                                          HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                          VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
                        <TextBlock x:Name="DropDownGlyph"
                                   Grid.Column="1"
                                   Margin="0,0,6,4"
                                   HorizontalAlignment="Right"
                                   VerticalAlignment="Center"
                                   FontFamily="{StaticResource SymbolThemeFontFamily}"
                                   FontSize="{StaticResource ComboBoxArrowThemeFontSize}"
                                   FontWeight="Bold"
                                   Foreground="Red"
                                   IsHitTestVisible="False"
                                   Text="" />
                        <Popup x:Name="Popup">
                            <Border x:Name="PopupBorder"
                                    HorizontalAlignment="Stretch"
                                    Background="{StaticResource ComboBoxPopupBackgroundThemeBrush}"
                                    BorderBrush="{StaticResource ComboBoxPopupBorderThemeBrush}"
                                    BorderThickness="{StaticResource ComboBoxPopupBorderThemeThickness}">
                                <ScrollViewer x:Name="ScrollViewer"
                                              BringIntoViewOnFocusChange="{TemplateBinding ScrollViewer.BringIntoViewOnFocusChange}"
                                              Foreground="{StaticResource ComboBoxPopupForegroundThemeBrush}"
                                              HorizontalScrollBarVisibility="{TemplateBinding ScrollViewer.HorizontalScrollBarVisibility}"
                                              HorizontalScrollMode="{TemplateBinding ScrollViewer.HorizontalScrollMode}"
                                              IsDeferredScrollingEnabled="{TemplateBinding ScrollViewer.IsDeferredScrollingEnabled}"
                                              IsHorizontalRailEnabled="{TemplateBinding ScrollViewer.IsHorizontalRailEnabled}"
                                              IsVerticalRailEnabled="{TemplateBinding ScrollViewer.IsVerticalRailEnabled}"
                                              VerticalScrollBarVisibility="{TemplateBinding ScrollViewer.VerticalScrollBarVisibility}"
                                              VerticalScrollMode="{TemplateBinding ScrollViewer.VerticalScrollMode}"
                                              VerticalSnapPointsAlignment="Near"
                                              VerticalSnapPointsType="OptionalSingle"
                                              ZoomMode="Disabled">
                                    <ItemsPresenter />
                                </ScrollViewer>
                            </Border>
                        </Popup>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

谢谢!

4

2 回答 2

1

要将 ComboBox 面板的大小设置为最大项目,请为 ComboBox.Width 设置一个显式值(或 ComboBox.MinWidth 设置最小值,但允许它针对较大的项目增长)。如果您提前知道最长项目的长度,则此方法有效。

ComboBox 的 CarouselPanel 继承自 VirtualizingPanel,因此由于项目可以虚拟化,因此很难确定最长的项目有多长。如果对您有用,设置显式宽度可能是最好的方法。

至于 ComboBox Popup 的位置,据我所知,定位逻辑是未通过 API 公开的实现细节。ComboBox 的设计遵循 Win8 的 ComboBox 设计指南,考虑到触摸、鼠标和键盘用户。用户对 ComboBox 的打开方式抱有期望,因此如果可能,最好遵循此处的指南。如果您确实需要实现上述自定义行为,您可以破解 ComboBox 控件来完成此操作或创建自定义解决方案。

于 2013-05-14T06:30:21.210 回答
0

您可以尝试将组合框的项目面板从 carouselpanel 更改为样式中的 stackpanel。这样,双方的虚拟化问题和组合框打开问题可能会得到解决。

(现在不能自己检查)

于 2013-05-16T18:49:46.010 回答