【WPF】修改ComboBox样式
修改WPF默认的ComboBox控件样式
如下图所示:

修改代码如下:
<UserControl.Resources>
<Style TargetType="ToggleButton" x:Key="stlToggleButton">
<Setter Property="Foreground" Value="White"></Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Border x:Name="Back" Background="#F7FDF7" BorderThickness="1" BorderBrush="Transparent">
<Path Name="PathFill" Fill="#59CA4F" Width="8" Height="6" StrokeThickness="0" Data="M5,0 L10,10 L0,10 z" RenderTransformOrigin="0.5,0.5" Stretch="Fill">
<Path.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform Angle="180"/>
<TranslateTransform/>
</TransformGroup>
</Path.RenderTransform>
</Path>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="PathFill" Property="Fill" Value="White"></Setter>
<Setter TargetName="Back" Property="Background" Value="#59CA4F"></Setter>
<Setter TargetName="Back" Property="BorderBrush" Value="#59CA4F"></Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="ComboBox" x:Key="stlComboBox">
<Setter Property="SnapsToDevicePixels" Value="True"/>
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
<Setter Property="ScrollViewer.CanContentScroll" Value="True"/>
<Setter Property="HorizontalAlignment" Value="Left"></Setter>
<Setter Property="Foreground" Value="Black"></Setter>
<Setter Property="Height" Value="30"></Setter>
<Setter Property="Margin" Value="0,0,0,0"></Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ComboBox">
<Grid Background="#F7FDF7">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.7*"/>
<ColumnDefinition Width="0.3*" MaxWidth="30"/>
</Grid.ColumnDefinitions>
<TextBox Grid.Column="0" IsReadOnly="True" Text="{TemplateBinding Text}"></TextBox>
<Border Grid.Column="0" BorderThickness="1,1,0,1" BorderBrush="#81D779" CornerRadius="1,0,0,1">
</Border>
<Border Grid.Column="1" BorderThickness="0,1,1,1" BorderBrush="#81D779" CornerRadius="0,1,1,0">
<ToggleButton Style="{StaticResource stlToggleButton}" IsChecked="{Binding Path=IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" ClickMode="Press"></ToggleButton>
</Border>
<Popup IsOpen="{TemplateBinding IsDropDownOpen}" Placement="Bottom" x:Name="Popup" Focusable="False" AllowsTransparency="True" PopupAnimation="Slide">
<Border CornerRadius="1" MaxHeight="{TemplateBinding MaxDropDownHeight}" MinWidth="{TemplateBinding ActualWidth}" x:Name="DropDown" SnapsToDevicePixels="True">
<Border.Effect>
<DropShadowEffect Color="Black" BlurRadius="2" ShadowDepth="0" Opacity="0.5"/>
</Border.Effect>
<ScrollViewer Margin="4,6,4,6" Style="{DynamicResource ScrollViewerStyle}" MaxHeight="{TemplateBinding MaxDropDownHeight}" SnapsToDevicePixels="True" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" CanContentScroll="True">
<!-- StackPanel 用于显示子级,方法是将 IsItemsHost 设置为 True -->
<StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained" Background="White"/>
</ScrollViewer>
</Border>
</Popup>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>
将上面代码插入到xaml文件中即可。然后在定义Combox控件的地方调用该样式即可。
调用方式有如何两种
①固定的调用方式:
<ComboBox x:Name="wpComBoxNew" Grid.Row="0" Style="{StaticResource stlComboBox}" Width="150" VerticalAlignment="Center" Margin="10,5,0,0" />
②动态的调用方式:
ComboBox combox = new ComboBox();
combox.Width = 180;
combox.Height = 30;
combox.Tag = rowIndex;
combox.ItemsSource = this.GetComboxItems;
combox.SelectedValuePath = "Key";
combox.DisplayMemberPath = "Value";
combox.Style = this.Resources["stlComboBox"] as Style;
combox.SelectedValue = _ocrTable.ColumnsDefinitions[rowIndex].Datatype.ToString();
combox.SelectionChanged += new SelectionChangedEventHandler(combox_SelectionChanged); this.wpComBox.Children.Add(combox);
上面这种在cs代码中实现的。实现样式的重点是下面这一句:
combox.Style = this.Resources["stlComboBox"] as Style;
参考博客为:http://blog.csdn.net/lvguoshan/article/details/49178619
【WPF】修改ComboBox样式的更多相关文章
- WPF 自定义ComboBox样式,自定义多选控件
原文:WPF 自定义ComboBox样式,自定义多选控件 一.ComboBox基本样式 ComboBox有两种状态,可编辑和不可编辑状态.通过设置IsEditable属性可以切换控件状态. 先看基本样 ...
- WPF 自定义ComboBox样式
一.ComboBox基本样式 ComboBox有两种状态,可编辑和不可编辑状态.通过设置IsEditable属性可以切换控件状态. 先看基本样式效果: 基本样式代码如下: <!--ComboBo ...
- WPF自定义控件与样式(8)-ComboBox与自定义多选控件MultComboBox
一.前言 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等,若有不明白的地方可以参考本系列前面的文章,文末附有部分文章链接. 本文主要内容: 下拉选 ...
- 【转】WPF自定义控件与样式(8)-ComboBox与自定义多选控件MultComboBox
一.前言 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等. 本文主要内容: 下拉选择控件ComboBox的自定义样式及扩展: 自定义多选控件Mul ...
- Wpf中鼠标样式的修改,作用点修改
最近,在做一个控件的鼠标样式,Ps加了插件,可以编辑生成.cur格式的图标. 可是,所有的改完以后,调试运行,结果发现自己制作的图标的作用点总是在左上角,而不是在"手形"图标的食指 ...
- WPF自定义控件与样式(1)-矢量字体图标(iconfont)
一.图标字体 图标字体在网页开发上运用非常广泛,具体可以网络搜索了解,网页上的运用有很多例子,如Bootstrap.但在C/S程序中使用还不多,字体图标其实就是把矢量图形打包到字体文件里,就像使用一般 ...
- WPF自定义控件与样式(9)-树控件TreeView与菜单Menu-ContextMenu
一.前言 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等,若有不明白的地方可以参考本系列前面的文章,文末附有部分文章链接. 本文主要内容: 菜单M ...
- WPF自定义控件与样式(14)-轻量MVVM模式实践
一.前言 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的,若有不明白的地方可以参考本系列前面的文章,文末附有部分文章链接. MVVM是WPF中一个非 ...
- [No000012F]WPF(7/7) - 样式,触发器和动画
WPF Tutorial : Beginning [^] WPF Tutorial : Layout-Panels-Containers & Layout Transformation [^] ...
随机推荐
- HYSBZ 1036(树的统计Count)
题目链接:传送门 题目大意:中文题,略 题目思路:树链剖分裸题. 闲谈:树链越练越熟练了 #include <iostream> #include <cstdio> #incl ...
- HDU 5306 Gorgeous Sequence[线段树区间最值操作]
Gorgeous Sequence Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ...
- vue 阻止表单默认的提交事件
form <form autocomplete="off" @submit.prevent="onSubmit"> <input type=& ...
- HDU2842—Chinese Rings
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2842 题目意思:一把一个n连环的前n个拿下来,一个个n连环,要把第k个拿下来,需要把前n-2个拿下来, ...
- the ONLY_FULL_GROUP_BY SQL mode is enabled (which it is by default) 用户变量
MySQL :: MySQL 8.0 Reference Manual :: 5.1.11 Server SQL Modes https://dev.mysql.com/doc/refman/8.0/ ...
- java获取src下文件
方式一: InputStream in = Test.class .getResourceAsStream("/env.properties"); URL url = Test.c ...
- Web爬虫的C#请求发送
public class HttpControler { //post请求发送 private Encoding m_Encoding = Encoding.GetEncoding("gb2 ...
- js-jquery-003-条形码-二维码【QR码】
一.基本使用 插件地址:https://github.com/jeromeetienne/jquery-qrcode 1.首先在页面中加入jquery库文件和qrcode插件. <script ...
- 17.如何网页发起QQ聊天
1.被发起的QQ需要再QQ推广开启QQ在线状态http://shang.qq.com/v3/widget.html 2.html代码 <html> <head></hea ...
- SIP UserAgent (B2BUA client)——pjsip
SIP UserAgent常用的SIP协议栈有pjsip/bell-sip/sofia-sip/libeXosip/libre等 https://github.com/staskobzar/sip_s ...