如何在ComboBox中设置下拉列表偏移量,使其像老派风格(win store app)

时间:2013-05-13 10:42:55

标签: windows xaml windows-8 windows-phone-8 winrt-xaml

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

我还希望下拉列表的宽度符合最大项目 - 永久,并且不要像现在那样动态更改(现在,根据视图中的项目调整大小)。

enter image description here

我尝试将comboBox的MaxDropDownHeight属性设置为0或300,但没有任何更改。

这是templateControl(图片与此代码段无关):

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

谢谢!

2 个答案:

答案 0 :(得分:0)

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

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

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

答案 1 :(得分:0)

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

(现在不能自己检查)