ComboBoxStyle.xaml 4.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  2. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  3. <ControlTemplate x:Key="ComboBoxToggleButton" TargetType="{x:Type ToggleButton}">
  4. <Grid x:Name="grid">
  5. <Grid.ColumnDefinitions>
  6. <ColumnDefinition />
  7. <ColumnDefinition Width="25" />
  8. </Grid.ColumnDefinitions>
  9. <Rectangle Grid.ColumnSpan="2" HorizontalAlignment="Stretch" x:Name="Rectangle" VerticalAlignment="Stretch" Width="Auto" Height="Auto" RadiusX="2" RadiusY="2" Fill="#f8f8f8" />
  10. <Border Margin="2,2,2,2" Grid.Column="1" Background="{StaticResource ThemeColor}" Width="20" Height="20" CornerRadius="3,3,3,3" x:Name="drop_border" />
  11. <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="White" Margin="5,0,5,0" Height="7" StrokeThickness="2" Stretch="Fill" />
  12. </Grid>
  13. </ControlTemplate>
  14. <ControlTemplate x:Key="ComboBoxTextBox" TargetType="{x:Type TextBox}">
  15. <Border x:Name="PART_ContentHost" Focusable="False" Background="{TemplateBinding Background}" />
  16. </ControlTemplate>
  17. <Style TargetType="{x:Type ComboBox}">
  18. <Setter Property="SnapsToDevicePixels" Value="true" />
  19. <Setter Property="Template">
  20. <Setter.Value>
  21. <ControlTemplate TargetType="{x:Type ComboBox}">
  22. <Border BorderBrush="Orange" x:Name="border">
  23. <Grid x:Name="grid">
  24. <ToggleButton Template="{StaticResource ComboBoxToggleButton}" x:Name="ToggleButton" Focusable="false" IsChecked="{Binding Path=IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" ClickMode="Press" />
  25. <ContentPresenter HorizontalAlignment="Left" Margin="5,0" x:Name="ContentSite" VerticalAlignment="Center" Content="{TemplateBinding SelectionBoxItem}" ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" IsHitTestVisible="False" />
  26. <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="{TemplateBinding Background}" />
  27. <!--文本输入框,当IsEditable为true 才显示-->
  28. <Popup IsOpen="{TemplateBinding IsDropDownOpen}" Placement="Bottom" x:Name="Popup" Focusable="False" AllowsTransparency="True" PopupAnimation="Slide">
  29. <Grid MaxHeight="{TemplateBinding MaxDropDownHeight}" MinWidth="{TemplateBinding ActualWidth}" x:Name="DropDown" SnapsToDevicePixels="True">
  30. <Border x:Name="DropDownBorder" Background="#f8f8f8" CornerRadius="3,3,3,3" />
  31. <ScrollViewer Margin="4,6,4,6" SnapsToDevicePixels="True" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" CanContentScroll="True" Foreground="{StaticResource {x:Static SystemColors.ActiveCaptionTextBrushKey}}">
  32. <StackPanel TextElement.FontSize="16" IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained" />
  33. </ScrollViewer>
  34. </Grid>
  35. </Popup>
  36. </Grid>
  37. </Border>
  38. <ControlTemplate.Triggers>
  39. <Trigger Property="HasItems" Value="false">
  40. <Setter Property="MinHeight" Value="95" TargetName="DropDownBorder" />
  41. </Trigger>
  42. <Trigger Property="IsGrouping" Value="true">
  43. <Setter Property="ScrollViewer.CanContentScroll" Value="false" />
  44. </Trigger>
  45. <Trigger Property="AllowsTransparency" SourceName="Popup" Value="true">
  46. <Setter Property="Margin" Value="0,2,0,0" TargetName="DropDownBorder" />
  47. </Trigger>
  48. <Trigger Property="IsEditable" Value="true">
  49. <Setter Property="IsTabStop" Value="false" />
  50. <Setter Property="Visibility" Value="Visible" TargetName="PART_EditableTextBox" />
  51. <Setter Property="Visibility" Value="Hidden" TargetName="ContentSite" />
  52. </Trigger>
  53. </ControlTemplate.Triggers>
  54. </ControlTemplate>
  55. </Setter.Value>
  56. </Setter>
  57. </Style>
  58. </ResourceDictionary>