WPF 简单的ComboBox自定义样式。

1 篇文章 0 订阅

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

点击下拉框之后:

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

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

 <ControlTemplate x:Key="MyToggleBtnStyle" 
                         TargetType="ToggleButton">
            <Border Name="MyBorder"
                    Background="AliceBlue"
                    BorderThickness="1" 
                    BorderBrush="LightGray">
                <Path Name="MyPath"
                      Fill="LightGray"
                      Height="10"
                      Width="10"
                      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"
                      Stretch="Fill">
                </Path>
            </Border>
            <ControlTemplate.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter TargetName="MyPath" Property="Fill" Value="#FF22A0E2"></Setter>
                    <Setter TargetName="MyBorder" Property="Background" Value="White"></Setter>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>

主要就是定义2个地方。

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

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

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

<ControlTemplate TargetType="ComboBox">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="7*"/>
                                <ColumnDefinition Width="3*" MaxWidth="20"/>
                            </Grid.ColumnDefinitions>
                            <Border Grid.Column="0"
                                    BorderBrush="LightGray"
                                    BorderThickness="1,1,0,1"
                                    Background="AliceBlue" CornerRadius="3 0 0 3">
                                <TextBox x:Name="myTxt"
                                         Text="{TemplateBinding Text}"
                                         Background="Transparent"
                                         BorderThickness="0"
                                         VerticalContentAlignment="Center"
                                         FontSize="14"
                                         FontWeight="Bold"
                                         Foreground="Blue"/>
                            </Border>
                            <Border Grid.Column="1"
                                    BorderBrush="LightGray"
                                    BorderThickness="1" CornerRadius="0 3 3 0">
                                <ToggleButton Content="&#xeda2;" IsChecked="{Binding Path=IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
                                              ClickMode="Press"
                                              Template="{StaticResource MyToggleBtnStyle}"></ToggleButton>
                            </Border>
                        </Grid>
                    </ControlTemplate>

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

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

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

 <Popup Name="MyPopup"
                                   IsOpen="{TemplateBinding IsDropDownOpen}"
                                   Placement="Bottom">
                                <Border MinWidth="{TemplateBinding ActualWidth}"
                                        MaxHeight="{TemplateBinding MaxDropDownHeight}">
                                    <ScrollViewer MaxHeight="{TemplateBinding MaxDropDownHeight}"
                                                  HorizontalScrollBarVisibility="Auto"
                                                  VerticalScrollBarVisibility="Auto">
                                        <StackPanel Background="AliceBlue"
                                                    IsItemsHost="True"/>
                                    </ScrollViewer>
                                </Border>
                            </Popup>

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

xmal:

<Window x:Class="ComboboxTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:ComboboxTest"
        mc:Ignorable="d"
        Title="ComboBoxStyle" Height="450" Width="800" Background="Green">
    <Window.Resources>
        <ControlTemplate x:Key="MyToggleBtnStyle" 
                         TargetType="ToggleButton">
            <Border Name="MyBorder"
                    Background="AliceBlue"
                    BorderThickness="1" 
                    BorderBrush="LightGray">
                <Path Name="MyPath"
                      Fill="LightGray"
                      Height="10"
                      Width="10"
                      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"
                      Stretch="Fill">
                </Path>
            </Border>
            <ControlTemplate.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter TargetName="MyPath" Property="Fill" Value="#FF22A0E2"></Setter>
                    <Setter TargetName="MyBorder" Property="Background" Value="White"></Setter>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>

        <Style x:Key="MyCbbStyle" TargetType="ComboBox">
            <Setter Property="BorderBrush" Value="#0e66fa"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ComboBox">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="7*"/>
                                <ColumnDefinition Width="3*" MaxWidth="20"/>
                            </Grid.ColumnDefinitions>
                            <Border Grid.Column="0"
                                    BorderBrush="LightGray"
                                    BorderThickness="1,1,0,1"
                                    Background="AliceBlue" CornerRadius="3 0 0 3">
                                <TextBox x:Name="myTxt"
                                         Text="{TemplateBinding Text}"
                                         Background="Transparent"
                                         BorderThickness="0"
                                         VerticalContentAlignment="Center"
                                         FontSize="14"
                                         FontWeight="Bold"
                                         Foreground="Blue"/>
                            </Border>
                            <Border Grid.Column="1"
                                    BorderBrush="LightGray"
                                    BorderThickness="1" CornerRadius="0 3 3 0">
                                <ToggleButton Content="&#xeda2;" IsChecked="{Binding Path=IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
                                              ClickMode="Press"
                                              Template="{StaticResource MyToggleBtnStyle}"></ToggleButton>
                            </Border>
                            <Popup Name="MyPopup"
                                   IsOpen="{TemplateBinding IsDropDownOpen}"
                                   Placement="Bottom">
                                <Border MinWidth="{TemplateBinding ActualWidth}"
                                        MaxHeight="{TemplateBinding MaxDropDownHeight}">
                                    <ScrollViewer MaxHeight="{TemplateBinding MaxDropDownHeight}"
                                                  HorizontalScrollBarVisibility="Auto"
                                                  VerticalScrollBarVisibility="Auto">
                                        <StackPanel Background="AliceBlue"
                                                    IsItemsHost="True"/>
                                    </ScrollViewer>
                                </Border>
                            </Popup>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    <Grid>
        <StackPanel Orientation="Vertical"
                    HorizontalAlignment="Center"
                    VerticalAlignment="Center">
            <Grid>
                <StackPanel Orientation="Vertical"
                    HorizontalAlignment="Center"
                    VerticalAlignment="Center">
                    <ComboBox Name="myCbb"
                      Height="25"
                      Width="250"
                      DisplayMemberPath="Name"
                      SelectedValuePath="ID"
                      SelectedIndex="0"
                      Style="{StaticResource MyCbbStyle}"/>
                </StackPanel>
            </Grid>
        </StackPanel>
    </Grid>

</Window>

.cs

using System.Windows;

namespace FontTest
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }

    public class CbbData
    {
        public string ID { get; set; }

        public string Name { get; set; }
    }
}

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

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

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
自定义 WPF ComboBox样式,步骤如下: 1. 创建一个新的 WPF 项目。 2. 在项目中打开 MainWindow.xaml 文件。 3. 在窗口中添加一个 ComboBox 控件。 4. 在 ComboBox 控件中添加一些项。 ```xml <ComboBox> <ComboBoxItem>Item 1</ComboBoxItem> <ComboBoxItem>Item 2</ComboBoxItem> <ComboBoxItem>Item 3</ComboBoxItem> </ComboBox> ``` 5. 现在我们将使用模板来自定义 ComboBox样式。右键单击 ComboBox 控件,选择“编辑模板”->“编辑复制的模板”->“创建”。 6. 在“对象和时间线”窗口中,找到名为“ComboBox”的控件模板。 7. 在模板中找到“Border”元素,并添加以下属性: ```xml <Border x:Name="border" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="3"> ``` 8. 在“ToggleButton”元素中添加以下属性: ```xml <ToggleButton x:Name="toggleButton" Grid.Column="2" Focusable="false" IsChecked="{Binding Path=IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" Template="{StaticResource ComboBoxToggleButton}"/> ``` 这将在 ComboBox 上添加一个箭头按钮。 9. 在“Popup”元素中添加以下属性,以更改下拉列表的背景颜色: ```xml <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" Background="White" BorderThickness="1" BorderBrush="Black" CornerRadius="3"/> <ScrollViewer Margin="4,6,4,6" SnapsToDevicePixels="True"> <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained"/> </ScrollViewer> </Grid> </Popup> ``` 10. 最后,添加以下样式到项目中: ```xml <Style x:Key="ComboBoxToggleButton" TargetType="{x:Type ToggleButton}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type ToggleButton}"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition/> <ColumnDefinition Width="20"/> </Grid.ColumnDefinitions> <Border x:Name="Border" Grid.ColumnSpan="2" CornerRadius="3" BorderThickness="1,1,1,1" Background="White" BorderBrush="Black"/> <Border Grid.Column="0" CornerRadius="3" Margin="1" Background="{TemplateBinding Background}" BorderThickness="0,0,0,0"/> <Path x:Name="Arrow" Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Center" Data="M 0 0 L 4 4 L 8 0 Z" Fill="Black"/> </Grid> <ControlTemplate.Triggers> <Trigger Property="ToggleButton.IsChecked" Value="True"> <Setter TargetName="Border" Property="Background" Value="#FFC1E0FF"/> <Setter TargetName="Border" Property="BorderBrush" Value="#FFA5CBE2"/> <Setter TargetName="Arrow" Property="Fill" Value="#FF000000"/> </Trigger> <Trigger Property="UIElement.IsEnabled" Value="False"> <Setter TargetName="Border" Property="Background" Value="#FFF4F4F4"/> <Setter TargetName="Border" Property="BorderBrush" Value="#FFADB2B5"/> <Setter TargetName="Arrow" Property="Fill" Value="#FF808080"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> ``` 11.运行程序,现在你将看到一个美丽的自定义 ComboBox 样式。 希望这能帮助到你!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

楚楚3107

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值