当前位置:   article > 正文

WPF 简单的ComboBox自定义样式。_wpf combox

wpf combox

先看下效果图,是不是自己想要的效果,点击之前:

点击下拉框之后:

好,如果满足自己的需求就继续往下看。如何制作一个简单的ComboBox样式。 先分解一下ComboBox的构成。由三部分组成,1 TextBlock,2 ToggleButton,3 Popup组成。如下图:

 那么分别作出三个控件的样式,组合在一起,就是一个简单的Combobx样式了。首先看一下ToggleButton的样式:

  1. <ControlTemplate x:Key="MyToggleBtnStyle"
  2. TargetType="ToggleButton">
  3. <Border Name="MyBorder"
  4. Background="AliceBlue"
  5. BorderThickness="1"
  6. BorderBrush="LightGray">
  7. <Path Name="MyPath"
  8. Fill="LightGray"
  9. Height="10"
  10. Width="10"
  11. Data="M29.917 8.6c-0.158-0.356-0.509-0.6-0.917-0.6h-26c-0.552 0-1 0.448-1 1 0 0.263 0.102 0.502 0.268 0.681l-0.001-0.001 13 14c0.183 0.197 0.444 0.32 0.733 0.32s0.55-0.123 0.732-0.319l0.001-0.001 13-14c0.166-0.178 0.267-0.417 0.267-0.68 0-0.145-0.031-0.282-0.086-0.406l0.003 0.006z"
  12. Stretch="Fill">
  13. </Path>
  14. </Border>
  15. <ControlTemplate.Triggers>
  16. <Trigger Property="IsMouseOver" Value="True">
  17. <Setter TargetName="MyPath" Property="Fill" Value="#FF22A0E2"></Setter>
  18. <Setter TargetName="MyBorder" Property="Background" Value="White"></Setter>
  19. </Trigger>
  20. </ControlTemplate.Triggers>
  21. </ControlTemplate>

主要就是定义2个地方。

1:一个方向向下的一个三角形。

2:鼠标滑过是的显示颜色。

TextBlock的显示样式:它被定义在了ComboBox样式中了。Combobox左边是TextBlock,右边是ToggleButton。

  1. <ControlTemplate TargetType="ComboBox">
  2. <Grid>
  3. <Grid.ColumnDefinitions>
  4. <ColumnDefinition Width="7*"/>
  5. <ColumnDefinition Width="3*" MaxWidth="20"/>
  6. </Grid.ColumnDefinitions>
  7. <Border Grid.Column="0"
  8. BorderBrush="LightGray"
  9. BorderThickness="1,1,0,1"
  10. Background="AliceBlue" CornerRadius="3 0 0 3">
  11. <TextBox x:Name="myTxt"
  12. Text="{TemplateBinding Text}"
  13. Background="Transparent"
  14. BorderThickness="0"
  15. VerticalContentAlignment="Center"
  16. FontSize="14"
  17. FontWeight="Bold"
  18. Foreground="Blue"/>
  19. </Border>
  20. <Border Grid.Column="1"
  21. BorderBrush="LightGray"
  22. BorderThickness="1" CornerRadius="0 3 3 0">
  23. <ToggleButton Content="&#xeda2;" IsChecked="{Binding Path=IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
  24. ClickMode="Press"
  25. Template="{StaticResource MyToggleBtnStyle}"></ToggleButton>
  26. </Border>
  27. </Grid>
  28. </ControlTemplate>

它是定义在ComboBox样式资源中的。对它的显示修改有几个地方。

1:CornerRadius。定义了左上,左下的圆角。CornerRadius="3 0 0 3" 它代表的意思是左上,右上,右下,左下。顺时针顺序的圆角角度。在把右边的ToggleButton 的CornerRadius="0 3 3 0" 右上,右下定义下圆角。这样整个ComboBox都是角度为3的圆角了。

Popup显示样式:基本没有改动,用原始的显示。

  1. <Popup Name="MyPopup"
  2. IsOpen="{TemplateBinding IsDropDownOpen}"
  3. Placement="Bottom">
  4. <Border MinWidth="{TemplateBinding ActualWidth}"
  5. MaxHeight="{TemplateBinding MaxDropDownHeight}">
  6. <ScrollViewer MaxHeight="{TemplateBinding MaxDropDownHeight}"
  7. HorizontalScrollBarVisibility="Auto"
  8. VerticalScrollBarVisibility="Auto">
  9. <StackPanel Background="AliceBlue"
  10. IsItemsHost="True"/>
  11. </ScrollViewer>
  12. </Border>
  13. </Popup>

好了。贴一下总的代码。总共两个页,一个xaml文件,一个.cs文件。

xmal:

  1. <Window x:Class="ComboboxTest.MainWindow"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  5. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  6. xmlns:local="clr-namespace:ComboboxTest"
  7. mc:Ignorable="d"
  8. Title="ComboBoxStyle" Height="450" Width="800" Background="Green">
  9. <Window.Resources>
  10. <ControlTemplate x:Key="MyToggleBtnStyle"
  11. TargetType="ToggleButton">
  12. <Border Name="MyBorder"
  13. Background="AliceBlue"
  14. BorderThickness="1"
  15. BorderBrush="LightGray">
  16. <Path Name="MyPath"
  17. Fill="LightGray"
  18. Height="10"
  19. Width="10"
  20. Data="M29.917 8.6c-0.158-0.356-0.509-0.6-0.917-0.6h-26c-0.552 0-1 0.448-1 1 0 0.263 0.102 0.502 0.268 0.681l-0.001-0.001 13 14c0.183 0.197 0.444 0.32 0.733 0.32s0.55-0.123 0.732-0.319l0.001-0.001 13-14c0.166-0.178 0.267-0.417 0.267-0.68 0-0.145-0.031-0.282-0.086-0.406l0.003 0.006z"
  21. Stretch="Fill">
  22. </Path>
  23. </Border>
  24. <ControlTemplate.Triggers>
  25. <Trigger Property="IsMouseOver" Value="True">
  26. <Setter TargetName="MyPath" Property="Fill" Value="#FF22A0E2"></Setter>
  27. <Setter TargetName="MyBorder" Property="Background" Value="White"></Setter>
  28. </Trigger>
  29. </ControlTemplate.Triggers>
  30. </ControlTemplate>
  31. <Style x:Key="MyCbbStyle" TargetType="ComboBox">
  32. <Setter Property="BorderBrush" Value="#0e66fa"/>
  33. <Setter Property="Template">
  34. <Setter.Value>
  35. <ControlTemplate TargetType="ComboBox">
  36. <Grid>
  37. <Grid.ColumnDefinitions>
  38. <ColumnDefinition Width="7*"/>
  39. <ColumnDefinition Width="3*" MaxWidth="20"/>
  40. </Grid.ColumnDefinitions>
  41. <Border Grid.Column="0"
  42. BorderBrush="LightGray"
  43. BorderThickness="1,1,0,1"
  44. Background="AliceBlue" CornerRadius="3 0 0 3">
  45. <TextBox x:Name="myTxt"
  46. Text="{TemplateBinding Text}"
  47. Background="Transparent"
  48. BorderThickness="0"
  49. VerticalContentAlignment="Center"
  50. FontSize="14"
  51. FontWeight="Bold"
  52. Foreground="Blue"/>
  53. </Border>
  54. <Border Grid.Column="1"
  55. BorderBrush="LightGray"
  56. BorderThickness="1" CornerRadius="0 3 3 0">
  57. <ToggleButton Content="&#xeda2;" IsChecked="{Binding Path=IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
  58. ClickMode="Press"
  59. Template="{StaticResource MyToggleBtnStyle}"></ToggleButton>
  60. </Border>
  61. <Popup Name="MyPopup"
  62. IsOpen="{TemplateBinding IsDropDownOpen}"
  63. Placement="Bottom">
  64. <Border MinWidth="{TemplateBinding ActualWidth}"
  65. MaxHeight="{TemplateBinding MaxDropDownHeight}">
  66. <ScrollViewer MaxHeight="{TemplateBinding MaxDropDownHeight}"
  67. HorizontalScrollBarVisibility="Auto"
  68. VerticalScrollBarVisibility="Auto">
  69. <StackPanel Background="AliceBlue"
  70. IsItemsHost="True"/>
  71. </ScrollViewer>
  72. </Border>
  73. </Popup>
  74. </Grid>
  75. </ControlTemplate>
  76. </Setter.Value>
  77. </Setter>
  78. </Style>
  79. </Window.Resources>
  80. <Grid>
  81. <StackPanel Orientation="Vertical"
  82. HorizontalAlignment="Center"
  83. VerticalAlignment="Center">
  84. <Grid>
  85. <StackPanel Orientation="Vertical"
  86. HorizontalAlignment="Center"
  87. VerticalAlignment="Center">
  88. <ComboBox Name="myCbb"
  89. Height="25"
  90. Width="250"
  91. DisplayMemberPath="Name"
  92. SelectedValuePath="ID"
  93. SelectedIndex="0"
  94. Style="{StaticResource MyCbbStyle}"/>
  95. </StackPanel>
  96. </Grid>
  97. </StackPanel>
  98. </Grid>
  99. </Window>

.cs

  1. using System.Windows;
  2. namespace FontTest
  3. {
  4. /// <summary>
  5. /// MainWindow.xaml 的交互逻辑
  6. /// </summary>
  7. public partial class MainWindow : Window
  8. {
  9. public MainWindow()
  10. {
  11. InitializeComponent();
  12. }
  13. }
  14. public class CbbData
  15. {
  16. public string ID { get; set; }
  17. public string Name { get; set; }
  18. }
  19. }

好了。一个ComboBox样式制作就介绍到这里,以后再介绍Button的自定义样式。

资源下载地址:https://download.csdn.net/download/chulijun3107/88414656

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小桥流水78/article/detail/928495
推荐阅读
相关标签
  

闽ICP备14008679号