WPFCombobox样式
<ControlTemplate x:Key="ComboBoxToggleButton" TargetType="{x:Type ToggleButton}">
<Grid x:Name="grid">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="20" />
</Grid.ColumnDefinitions>
<Rectangle Grid.ColumnSpan="2" HorizontalAlignment="Stretch" x:Name="Rectangle" VerticalAlignment="Stretch" Width="Auto" Height="Auto" RadiusX="3" RadiusY="3" Fill="LightBlue" /> <!--Combobox控件外壳-->
<Rectangle Grid.Column="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="Auto" Height="Auto" RadiusX="3" RadiusY="3" Fill="Gray" /> <!--除开下拉按钮的其他部分-->
<Border Margin="2,2,2,2" Grid.Column="1" Background="Red" Width="Auto" CornerRadius="3,3,3,3" x:Name="drop_border" />
<Path Grid.Column="1" HorizontalAlignment="Center" Width="Auto" x:Name="Arrow" VerticalAlignment="Center" Fill="{x:Null}" Data="M0.5,0.5 L3,6.5 5.5,0.5" Stroke="Black" Margin="5,0,5,0" Height="7" StrokeThickness="2" Stretch </Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter TargetName="drop_border" Property="Background" Value="White" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
<ControlTemplate x:Key="ComboBoxTextBox" TargetType="{x:Type TextBox}">
<Border x:Name="PART_ContentHost" Focusable="False" Background="{TemplateBinding Background}" />
</ControlTemplate>
<Style TargetType="{x:Type ComboBox}">
<Setter Property="SnapsToDevicePixels" Value="true" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ComboBox}">
<Border BorderBrush="Orange" x:Name="border">
<Grid x:Name="grid">
<ToggleButton Template="{StaticResource ComboBoxToggleButton}" x:Name="ToggleButton" Focusable="false" IsChecked="{Binding Path=IsDropDownOpen, Mode=TwoWay, RelativeSource={
RelativeSource TemplatedP <ContentPresenter HorizontalAlignment="Left" x:Name="ContentSite" VerticalAlignment="Center" Content="{TemplateBinding SelectionBoxItem}" ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" ContentT <TextBox Visibility="Hidden" Margin="2,2,22,2" x:Name="PART_EditableTextBox" Style="{x:Null}" VerticalAlignment="Center" Focusable="True" IsReadOnly="{TemplateBinding IsReadOnly}" Foreground="Black" Horizonta <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="WhiteSmoke" CornerRadius="3,3,3,3" />
<ScrollViewer Margin="4,6,4,6" SnapsToDevicePixels="True" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" CanContentScroll="True" Foreground="{StaticResource {x:Static SystemColors.Activ <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained" />
</ScrollViewer>
</Grid>
</Popup>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="HasItems" Value="false">
<Setter Property="MinHeight" Value="95" TargetName="DropDownBorder" />
</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>
</Setter.Value>
</Setter>
</Style>
⼀直没搞懂Combobox具体是怎么设计的,今天抽空研究了⼀下,终于看懂了,主要有3个控件组成,ToggleButton,TextBox 和Popup,
ToggleButton:整个控件是由ToggleButton铺满的HorizontalAlignment="Stretch",ToggleButton⾥⾯由⼀个Rectangle铺满,设置Combobox的圆⾓,边框及背景颜⾊,然后宽度20px的Border,path 为下拉按钮样式,点击combobox
下拉按钮实际是点击ToggleButton
TextBox :当IsEditable为True,会把Textbox 显⽰出来,Textbox是覆盖在ToggleButton之上的,注意应设置HorizontalAlignment="Stretch",这样才会把Textbox铺满,⽹上很多设置的是Left,很不好选中
TextBox,其次还应设
置Margin="2,2,22,2",距右边22px,这样才不能遮挡出下拉按钮
Popup:⽤于显⽰ComboboxItem
<ControlTemplate x:Key="ComboBoxToggleButton" TargetType="{x:Type ToggleButton}"> <Grid x:Name="grid"> <Grid.ColumnDefinitions>
<ColumnDefinition /> <ColumnDefinition Width="20" /> </Grid.ColumnDefinitions> <Rectangle Grid.ColumnSpan="2"
HorizontalAlignment="Stretch" x:Name="Rectangle" VerticalAlignment="Stretch" Width="Auto" Height="Auto" RadiusX="3" RadiusY="3" Fill="LightBlue" /> <!--Combobox控件
外壳--> <Rectangle Grid.Column="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="Auto" Height="Auto" RadiusX="3" RadiusY="3" Fill="Gray" />
<!--除开下拉按钮的其他部分--> <Border Margin="2,2,2,2" Grid.Column="1" Background="Red" Width="Auto" CornerRadius="3,3,3,3" x:Name="drop_border" />
<Path Grid.Column="1" HorizontalAlignment="Center" Width="Auto" x:Name="Arrow" VerticalAlignment="Center" Fill="{x:Null}" Data="M0.5,0.5 L3,6.5 5.5,0.5"
Stroke="Black" Margin="5,0,5,0" Height="7" StrokeThickness="2" Stretch="Fill" /> <!--Border 和 Path为下拉按钮--> </Grid> <ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="true"> <Setter TargetName="drop_border" Property="Background" Value="White" /> </Trigger>
</ControlTemplate.Triggers> </ControlTemplate>
<ControlTemplate x:Key="ComboBoxTextBox" TargetType="{x:Type TextBox}"> <Border x:Name="PART_ContentHost" Focusable="False" Background="
{TemplateBinding Background}" /> </ControlTemplate> <Style TargetType="{x:Type ComboBox}"> <Setter Property="SnapsToDevicePixels" Value="true" />
<Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type ComboBox}"> <Border BorderBrush="Orange"
x:Name="border"> <Grid x:Name="grid"> <ToggleButton Template="{StaticResource ComboBoxToggleButton}" x:Name="ToggleButton"
Focusable="false" IsChecked="{Binding Path=IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" ClickMode="Press" />
borderbox<ContentPresenter HorizontalAlignment="Left" x:Name="ContentSite" VerticalAlignment="Center" Content="{TemplateBinding SelectionBoxItem}" ContentTemplate="
{TemplateBinding SelectionBoxItemTemplate}" ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" IsHitTestVisible="False" /> <TextBox
Visibility="Hidden" Margin="2,2,22,2" x:Name="PART_EditableTextBox" Style="{x:Null}" VerticalAlignment="Center" Focusable="True" IsReadOnly="{TemplateBinding
IsReadOnly}" Foreground="Black" HorizontalAlignment="Stretch" Background="Azure" /> <!--⽂本输⼊框,当IsEditable为true 才显⽰--> <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="WhiteSmoke" CornerRadius="3,3,3,3" /> <ScrollViewer Margin="4,6,4,6"
SnapsToDevicePixels="True" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" CanContentScroll="True" Foreground="{StaticResource {x:Static
SystemColors.ActiveCaptionTextBrushKey}}"> <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained" />
</ScrollViewer> </Grid> </Popup> </Grid> </Border> <ControlTemplate.Triggers>
<Trigger Property="HasItems" Value="false"> <Setter Property="MinHeight" Value="95" TargetName="DropDownBorder" />
</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> </Setter.Value> </Setter> </Style>
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论