默认combobox的scrollbar在右边,我想让他到左边一点,如何实现呢?
看了下controltemplate里面没有scrollbar的选项啊,求指教,先谢谢了~

解决方案 »

  1.   

    回楼上,类似自定义控件,只不过是想让scrollbar的位置改到左边去,没什么很好的思路
      

  2.   

    只是改变位置,这个应该比较简单的,只需自定义模板就OK了,方法如下:左边显示滚动条的ScrollViewer模板代码: <Style x:Key="LeftScrollViewer"
             TargetType="{x:Type ScrollViewer}">
        <Setter Property="OverridesDefaultStyle"
                Value="True" />
        <Setter Property="Template">
          <Setter.Value>
            <ControlTemplate TargetType="{x:Type ScrollViewer}">
              <Grid>
                <Grid.ColumnDefinitions>
                  <ColumnDefinition Width="Auto" />
                  <ColumnDefinition />
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                  <RowDefinition />
                  <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>
                <Border Grid.Column="1"
                        BorderThickness="0,1,1,1">
                  <Border.BorderBrush>
                    <SolidColorBrush Color="{DynamicResource BorderMediumColor}" />
                  </Border.BorderBrush>
                  <ScrollContentPresenter />
                </Border>
                <ScrollBar x:Name="PART_VerticalScrollBar"
                           Value="{TemplateBinding VerticalOffset}"
                           Maximum="{TemplateBinding ScrollableHeight}"
                           ViewportSize="{TemplateBinding ViewportHeight}"
                           Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}"/>
                <ScrollBar x:Name="PART_HorizontalScrollBar"
                           Orientation="Horizontal"
                           Grid.Row="1"
                           Grid.Column="1"
                           Value="{TemplateBinding HorizontalOffset}"
                           Maximum="{TemplateBinding ScrollableWidth}"
                           ViewportSize="{TemplateBinding ViewportWidth}"
                           Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}"/>          </Grid>
            </ControlTemplate>
          </Setter.Value>
        </Setter>
      </Style>
    然后修改Combobox弹出框中ScrollViewer的样式属性:Style="{StaticResource LeftScrollViewer}"以下是我测试用的Combobox的完整样式模板,如果没有自定义模板,则使用Blend生成一个默认的副本模板:<Style x:Key="{x:Type ComboBox}"
             TargetType="{x:Type ComboBox}">
        <Setter Property="SnapsToDevicePixels"
                Value="true" />
        <Setter Property="OverridesDefaultStyle"
                Value="true" />
        <Setter Property="ScrollViewer.HorizontalScrollBarVisibility"
                Value="Auto" />
        <Setter Property="ScrollViewer.VerticalScrollBarVisibility"
                Value="Auto" />
        <Setter Property="ScrollViewer.CanContentScroll"
                Value="true" />
        <Setter Property="MinWidth"
                Value="50" />
        <Setter Property="MinHeight"
                Value="20" />
        <Setter Property="Template">
          <Setter.Value>
            <ControlTemplate TargetType="{x:Type ComboBox}">
              <Grid>
                <VisualStateManager.VisualStateGroups>
                  <VisualStateGroup x:Name="CommonStates">
                    <VisualState x:Name="Normal" />
                    <VisualState x:Name="MouseOver" />
                    <VisualState x:Name="Disabled">
                      <Storyboard>
                        <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(TextElement.Foreground).(SolidColorBrush.Color)" Storyboard.TargetName="PART_EditableTextBox">
                         <EasingColorKeyFrame KeyTime="0" Value="{StaticResource DisabledForegroundColor}"/>
                        </ColorAnimationUsingKeyFrames>
                      </Storyboard>
                    </VisualState>
                  </VisualStateGroup>
                  <VisualStateGroup x:Name="EditStates">
                    <VisualState x:Name="Editable">
                      <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)"
                                                       Storyboard.TargetName="PART_EditableTextBox">
                          <DiscreteObjectKeyFrame KeyTime="0"
                                                  Value="{x:Static Visibility.Visible}" />
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames
                            Storyboard.TargetProperty="(UIElement.Visibility)"
                                                       Storyboard.TargetName="ContentSite">
                          <DiscreteObjectKeyFrame KeyTime="0"
                                                  Value="{x:Static Visibility.Hidden}" />
                        </ObjectAnimationUsingKeyFrames>
                      </Storyboard>
                    </VisualState>
                    <VisualState x:Name="Uneditable" />
                  </VisualStateGroup>
                </VisualStateManager.VisualStateGroups>
                <ToggleButton x:Name="ToggleButton"
                              Template="{StaticResource ComboBoxToggleButton}"
                              Grid.Column="2"
                              Focusable="false"
                              ClickMode="Press"
                              IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, 
                    RelativeSource={RelativeSource TemplatedParent}}"/>
                <ContentPresenter x:Name="ContentSite"
                                  IsHitTestVisible="False"
                                  Content="{TemplateBinding SelectionBoxItem}"
                                  ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"
                                  ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"
                                  Margin="3,3,23,3"
                                  VerticalAlignment="Stretch"
                                  HorizontalAlignment="Left">
                </ContentPresenter>
                <TextBox x:Name="PART_EditableTextBox"
                         Style="{x:Null}"
                         Template="{StaticResource ComboBoxTextBox}"
                         HorizontalAlignment="Left"
                         VerticalAlignment="Bottom"
                         Margin="3,3,23,3"
                         Focusable="True"
                         Background="Transparent"
                         Visibility="Hidden"
                         IsReadOnly="{TemplateBinding IsReadOnly}" />
                <Popup x:Name="Popup"
                       Placement="Bottom"
                       IsOpen="{TemplateBinding IsDropDownOpen}"
                       AllowsTransparency="True"
                       Focusable="False"
                       PopupAnimation="Slide">
                  <Grid x:Name="DropDown"
                        SnapsToDevicePixels="True"
                        MinWidth="{TemplateBinding ActualWidth}"
                        MaxHeight="{TemplateBinding MaxDropDownHeight}">
                    <Border x:Name="DropDownBorder"
                            BorderThickness="1">
                      <Border.BorderBrush>
                        <SolidColorBrush Color="{DynamicResource BorderMediumColor}" />
                      </Border.BorderBrush>
                      <Border.Background>
                        <SolidColorBrush Color="{DynamicResource ControlLightColor}" />
                      </Border.Background>
                    </Border>
                    <ScrollViewer Margin="4,6,4,6" Style="{StaticResource LeftScrollViewer}" SnapsToDevicePixels="True">
                      <StackPanel IsItemsHost="True"
                                  KeyboardNavigation.DirectionalNavigation="Contained" />
                    </ScrollViewer>
                  </Grid>
                </Popup>
              </Grid>
              <ControlTemplate.Triggers>
                <Trigger Property="HasItems"
                         Value="false">
                  <Setter TargetName="DropDownBorder"
                          Property="MinHeight"
                          Value="95" />
                </Trigger>
                <Trigger Property="IsGrouping"
                         Value="true">
                  <Setter Property="ScrollViewer.CanContentScroll"
                          Value="false" />
                </Trigger>
                <Trigger SourceName="Popup"
                         Property="AllowsTransparency"
                         Value="true">
                  <Setter TargetName="DropDownBorder"
                          Property="CornerRadius"
                          Value="4" />
                  <Setter TargetName="DropDownBorder"
                          Property="Margin"
                          Value="0,2,0,0" />
                </Trigger>
              </ControlTemplate.Triggers>
            </ControlTemplate>
          </Setter.Value>
        </Setter>
      </Style>
      

  3.   

    上面方法是针对Combobox弹出框定义的ScrollBar 的位置,如果要将所有ScrollBar 位置都放在左边,则将上面示例中ScrollViewer的样式定义为全局样式就OK了,这种方式可以不用修改Combobox的样式
      

  4.   

    感谢楼上的回复,可能我的措词有点让人误解,我说的左边去不是完全到左边
    默认combobox的scrollbar是在item的外面的,我只是想让他到item里面去,也就是往左一点点的意思、、、
      

  5.   


    实现原理一样,修改ScrollViewer的布局就可以实现了,其他步骤都一样
      

  6.   

    注意我的示例中ScrollViewer的模板,<ScrollContentPresenter/>是要滚动的内容,在Combobox中就是下拉框的内容,Grid作为布局容器,两个ScrollBar分别是横向滚动条(Grid中1行1列)和纵向滚动条(Grid中0行0列),如果你对wpf的布局系统有一定的了解,那么可以很方便的实现任何所需的布局要求
      

  7.   

    如果让ComboBox 中的Vertical scroll bar 出现的位置总是处于TOP的位置,应该怎么做? 谢谢,求解决办法