none
ComboBox 下拉列表(四角)可以做成圆角状的吗?该如何处理呢? RRS feed

答案

  • 修改ComboBox的Template, 在VS 2010或者Blend中你可以导出ComboBox的默认模板:

    VS2010中:

    然后修改里面的模板,比如:

    <Window x:Class="WpfApplication3.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525" xmlns:my="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero">
      <Window.Resources>
        <ControlTemplate x:Key="Template1" TargetType="ComboBox">
          <Grid Name="MainGrid" SnapsToDevicePixels="True">
            <Grid.ColumnDefinitions>
              <ColumnDefinition Width="*" />
              <ColumnDefinition MinWidth="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}" Width="0" />
            </Grid.ColumnDefinitions>
            <Popup AllowsTransparency="True" Grid.ColumnSpan="2" IsOpen="{Binding Path=IsDropDownOpen, RelativeSource={RelativeSource TemplatedParent}}" Margin="1" Name="PART_Popup" Placement="Bottom" PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}">
              <my:SystemDropShadowChrome Color="Transparent" MaxHeight="{TemplateBinding ComboBox.MaxDropDownHeight}" MinWidth="{Binding Path=ActualWidth, ElementName=MainGrid}" Name="Shdw">
                <Border Background="{DynamicResource {x:Static SystemColors.WindowBrushKey}}" BorderThickness="1" Name="DropDownBorder"
                        CornerRadius="10" BorderBrush="Blue"> <!-- Here -->
                  <ScrollViewer Name="DropDownScrollViewer" Margin="5"><!-- Here -->
                    <Grid RenderOptions.ClearTypeHint="Enabled">
                      <Canvas Height="0" HorizontalAlignment="Left" VerticalAlignment="Top" Width="0">
                        <Rectangle Fill="{Binding Path=Background, ElementName=DropDownBorder}" Height="{Binding Path=ActualHeight, ElementName=DropDownBorder}" Name="OpaqueRect" Width="{Binding Path=ActualWidth, ElementName=DropDownBorder}" />
                      </Canvas>
                      <ItemsPresenter KeyboardNavigation.DirectionalNavigation="Contained" Name="ItemsPresenter" SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />
                    </Grid>
                  </ScrollViewer>
                </Border>
              </my:SystemDropShadowChrome>
            </Popup>
            <ToggleButton Background="{TemplateBinding Control.Background}" BorderBrush="{TemplateBinding Control.BorderBrush}" Grid.ColumnSpan="2" IsChecked="{Binding Path=IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}">
              <ToggleButton.Style>
                <Style TargetType="ToggleButton">
                  <Setter Property="FrameworkElement.OverridesDefaultStyle" Value="True" />
                  <Setter Property="Control.IsTabStop" Value="False" />
                  <Setter Property="UIElement.Focusable" Value="False" />
                  <Setter Property="ButtonBase.ClickMode" Value="Press" />
                  <Setter Property="Control.Template">
                    <Setter.Value>
                      <ControlTemplate TargetType="ToggleButton">
                        <my:ButtonChrome Background="{TemplateBinding Control.Background}" BorderBrush="{TemplateBinding Control.BorderBrush}" Name="Chrome" RenderMouseOver="{TemplateBinding UIElement.IsMouseOver}" RenderPressed="{TemplateBinding ButtonBase.IsPressed}" SnapsToDevicePixels="True">
                          <Grid HorizontalAlignment="Right" Width="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}">
                            <Path Data="M0,0L3.5,4 7,0z" Fill="Black" HorizontalAlignment="Center" Margin="3,1,0,0" Name="Arrow" VerticalAlignment="Center" />
                          </Grid>
                        </my:ButtonChrome>
                        <ControlTemplate.Triggers>
                          <Trigger Property="ToggleButton.IsChecked" Value="True">
                            <Setter Property="my:ButtonChrome.RenderPressed" TargetName="Chrome" Value="True" />
                          </Trigger>
                          <Trigger Property="UIElement.IsEnabled" Value="False">
                            <Setter Property="Shape.Fill" TargetName="Arrow" Value="#FFAFAFAF" />
                          </Trigger>
                        </ControlTemplate.Triggers>
                      </ControlTemplate>
                    </Setter.Value>
                  </Setter>
                </Style>
              </ToggleButton.Style>
            </ToggleButton>
            <ContentPresenter Content="{TemplateBinding ComboBox.SelectionBoxItem}" ContentStringFormat="{TemplateBinding ComboBox.SelectionBoxItemStringFormat}" ContentTemplate="{TemplateBinding ComboBox.SelectionBoxItemTemplate}" HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}" IsHitTestVisible="False" Margin="{TemplateBinding Control.Padding}" SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding Control.VerticalContentAlignment}" />
          </Grid>
          <ControlTemplate.Triggers>
            <Trigger Property="Popup.HasDropShadow" SourceName="PART_Popup" Value="True">
              <Setter Property="FrameworkElement.Margin" TargetName="Shdw" Value="0,0,5,5" />
              <Setter Property="my:SystemDropShadowChrome.Color" TargetName="Shdw" Value="#71000000" />
            </Trigger>
            <Trigger Property="ItemsControl.HasItems" Value="False">
              <Setter Property="FrameworkElement.Height" TargetName="DropDownBorder" Value="95" />
            </Trigger>
            <Trigger Property="UIElement.IsEnabled" Value="False">
              <Setter Property="Control.Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
              <Setter Property="Control.Background" Value="#FFF4F4F4" />
            </Trigger>
            <Trigger Property="ItemsControl.IsGrouping" Value="True">
              <Setter Property="ScrollViewer.CanContentScroll" Value="False" />
            </Trigger>
            <Trigger Property="ScrollViewer.CanContentScroll" SourceName="DropDownScrollViewer" Value="False">
              <Setter Property="Canvas.Top" TargetName="OpaqueRect" Value="{Binding Path=VerticalOffset, ElementName=DropDownScrollViewer}" />
              <Setter Property="Canvas.Left" TargetName="OpaqueRect" Value="{Binding Path=HorizontalOffset, ElementName=DropDownScrollViewer}" />
            </Trigger>
          </ControlTemplate.Triggers>
        </ControlTemplate>
      </Window.Resources>
      <Grid>
        <ComboBox Height="23" HorizontalAlignment="Left" Name="comboBox1" VerticalAlignment="Top" Width="120" Template="{StaticResource Template1}" />
      </Grid>
    </Window>
    
    

     


    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us

    2011年12月23日 8:45
    版主