WPF自定义选择年月控件详解
本文实例为大家分享了WPF自定义选择年月控件的具体代码,供大家参考,具体内容如下
封装了一个选择年月的控件,XAML代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
< UserControl x:Class = "SunCreate.CombatPlatform.Client.DateMonthPicker" Height = "23" Loaded = "UserControl_Loaded" > < UserControl.Resources > < ResourceDictionary > < ResourceDictionary.MergedDictionaries > < ResourceDictionary Source = "/SunCreate.CombatPlatform.Client.Resources;Component/Resource/DateTimePickerResource.xaml" /> </ ResourceDictionary.MergedDictionaries > < Style TargetType = "ToggleButton" x:Key = "stlToggleButton" > < Setter Property = "Foreground" Value = "White" ></ Setter > < Setter Property = "Template" > < Setter.Value > < ControlTemplate > < Border x:Name = "Back" Background = "Transparent" BorderThickness = "0" BorderBrush = "Transparent" > < Path Name = "PathFill" Fill = "#1b94e0" 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 = "#1b94e0" ></ Setter > < Setter TargetName = "Back" Property = "Background" Value = "Transparent" ></ Setter > < Setter TargetName = "Back" Property = "BorderBrush" Value = "Transparent" ></ 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 > < Grid.Background > < ImageBrush ImageSource = "/SunCreate.CombatPlatform.Client.Resources;component/Image/Face/1比n人脸比对/输入框.png" /> </ Grid.Background > < Grid.ColumnDefinitions > < ColumnDefinition Width = "0.7*" /> < ColumnDefinition Width = "0.3*" MaxWidth = "30" MinWidth = "18" /> </ Grid.ColumnDefinitions > < TextBox Grid.Column = "0" IsReadOnly = "True" Foreground = "#1ba4f6" BorderThickness = "1" BorderBrush = "Transparent" Text = "{TemplateBinding Text}" Background = "Transparent" ></ TextBox > < Border Grid.Column = "0" BorderThickness = "0" Background = "Transparent" > </ Border > < Border Grid.Column = "1" BorderThickness = "0" CornerRadius = "0,1,1,0" Background = "Transparent" > < 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" Background = "Transparent" > < Border.Effect > < DropShadowEffect Color = "#1ba4f6" 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 = "#1ba4f6" /> </ ScrollViewer > </ Border > </ Popup > </ Grid > </ ControlTemplate > </ Setter.Value > </ Setter > </ Style > </ ResourceDictionary > </ UserControl.Resources > < Grid > < StackPanel Orientation = "Horizontal" > < ComboBox Grid.Column = "2" Grid.Row = "0" Name = "cbYear" SelectionChanged = "cbYear_SelectionChanged" SelectedValuePath = "Text" DisplayMemberPath = "Text" Height = "25" Width = "55" Style = "{StaticResource stlComboBox}" VerticalAlignment = "Center" > </ ComboBox > < TextBlock Text = "年" Margin = "5 0 5 0" VerticalAlignment = "Center" Foreground = "#1ba4f6" /> < ComboBox Grid.Column = "2" Grid.Row = "0" Name = "cbMonth" SelectionChanged = "cbMonth_SelectionChanged" SelectedValuePath = "Text" DisplayMemberPath = "Text" Height = "25" Width = "40" Style = "{StaticResource stlComboBox}" VerticalAlignment = "Center" > </ ComboBox > < TextBlock Text = "月" Margin = "5 0 5 0" VerticalAlignment = "Center" Foreground = "#1ba4f6" /> </ StackPanel > </ Grid > </ UserControl > |
后台代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.ComponentModel; namespace SunCreate.CombatPlatform.Client { /// <summary> /// /// </summary> public partial class DateMonthPicker : UserControl, INotifyPropertyChanged { private DateTime _selectedMonth; public static DependencyProperty selectedTimeProperty; static DateMonthPicker() { selectedTimeProperty = DependencyProperty.Register( "SelectedMonth" , typeof (DateTime), typeof (DateMonthPicker), new PropertyMetadata(DateTime.Now, new PropertyChangedCallback(SelectedMonthChanged))); } public DateMonthPicker() { InitializeComponent(); int currentYear = DateTime.Now.Year; int currentMonth = DateTime.Now.Month; List< object > yearList = new List< object >(); for ( int i = currentYear - 20; i <= currentYear; i++) { yearList.Add( new { Text = i.ToString() }); } cbYear.ItemsSource = yearList; cbMonth.ItemsSource = new List< object >() { new { Text = "1" }, new { Text = "2" }, new { Text = "3" }, new { Text = "4" }, new { Text = "5" }, new { Text = "6" }, new { Text = "7" }, new { Text = "8" }, new { Text = "9" }, new { Text = "10" }, new { Text = "11" }, new { Text = "12" }}; this ._selectedMonth = DateTime.Now; } private void UserControl_Loaded( object sender, RoutedEventArgs e) { cbYear.SelectedValue = _selectedMonth.Year.ToString(); cbMonth.SelectedValue = _selectedMonth.Month.ToString(); } private static void SelectedMonthChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e) { (obj as DateMonthPicker).ChangeSelect(e.NewValue); } private void ChangeSelect( object value) { _selectedMonth = (DateTime)value; cbYear.SelectedValue = _selectedMonth.Year.ToString(); cbMonth.SelectedValue = _selectedMonth.Month.ToString(); } public DateTime SelectedMonth { get { return (DateTime) this .GetValue(DateMonthPicker.selectedTimeProperty); } set { this .SetValue(DateMonthPicker.selectedTimeProperty, value); } } public DateTime StartDay { get { return this ._selectedMonth.AddDays(1 - this ._selectedMonth.Day).Date; } } public DateTime EndDay { get { return this .StartDay.AddMonths(1).AddDays(-1); } } #region INotifyPropertyChanged 成员 public event PropertyChangedEventHandler PropertyChanged; private void SendPropertyChanged(String propertyName) { if (PropertyChanged != null ) this .PropertyChanged( this , new PropertyChangedEventArgs(propertyName)); } #endregion private void cbYear_SelectionChanged( object sender, SelectionChangedEventArgs e) { ComboBox cb = sender as ComboBox; if ( this ._selectedMonth != DateTime.MinValue && cb.SelectedValue != null ) { this ._selectedMonth = new DateTime(Convert.ToInt32(cb.SelectedValue), this ._selectedMonth.Month, 1); SelectedMonth = this ._selectedMonth; } } private void cbMonth_SelectionChanged( object sender, SelectionChangedEventArgs e) { ComboBox cb = sender as ComboBox; if ( this ._selectedMonth != DateTime.MinValue && cb.SelectedValue != null ) { this ._selectedMonth = new DateTime( this ._selectedMonth.Year, Convert.ToInt32(cb.SelectedValue), 1); SelectedMonth = this ._selectedMonth; } } } } |
效果图:
WPF自定义选择年月控件详解的更多相关文章
- IOS—UITextFiled控件详解
IOS—UITextFiled控件详解 //初始化textfield并设置位置及大小 UITextField *text = [[UITextField alloc]initWithFrame:CGR ...
- ToolBar控件详解
ToolBar控件详解 在Activity中添加ToolBar 1.添加库 dependencies { ... compile "com.android.support:appcompat ...
- Spinner控件详解
Spinner控件详解 效果图 修改Spinner样式 在介绍之前,先看一下系统原生的样式 6.x & 5.x系统样式 4.x系统样式 官方文档 XML属性 方法 描述 android:dro ...
- Android开发:文本控件详解——TextView(一)基本属性
一.简单实例: 新建的Android项目初始自带的Hello World!其实就是一个TextView. 在activity_main.xml中可以新建TextView,从左侧组件里拖拽到右侧预览界面 ...
- C++ CComboBox控件详解
转载:http://blog.sina.com.cn/s/blog_46d93f190100m395.html C++ CComboBox控件详解 (2010-09-14 14:03:44) 转载▼ ...
- picker控件详解与使用,(实现省市的二级联动)
picker控件详解与使用,(实现省市的二级联动) 第一步:新建一个单视图(single view)的工程, 命名为pickerTest,不要勾选下面两个选项,第一个是新版本里面的,第二个是单元测试, ...
- Switch控件详解
Switch控件详解 原生效果 5.x 4.x 布局 <Switch android:id="@+id/setting_switch" android:layout_widt ...
- Android开发:文本控件详解——TextView(二)文字跑马灯效果实现
一.需要使用的属性: 1.android:ellipsize 作用:若文字过长,控制该控件如何显示. 对于同样的文字“Android开发:文本控件详解——TextView(二)文字跑马灯效果实现”,不 ...
- Android输入控件详解
输入控件 输入控件是您的应用用户界面中的交互式组件.Android 提供了多种可在 UI 中使用的控件,如按钮.文本字段.定位栏.复选框.缩放按钮.切换按钮等. 向 UI 中添加输入控件与向 XML ...
随机推荐
- Matcher类详解2-group
Matcher.group是针对()来说的,group(0)就是指的整个串,group(1) 指的是第一个括号里的东西即匹配的第一个子表达式,group(2)指的第二个括号里的东西即匹配的第二个子表达 ...
- Scrapy笔记:使用代理ip
scrapy框架使用代理ip的基本思路是修改请求对象中的meta['proxy']的值,将代理ip赋值给这个属性.遵循这个思路,只要是生成Request对象的地方都可以设置Request的值. dow ...
- localStorage.getItem
WEB应用的快速发展,是的本地存储一些数据也成为一种重要的需求,实现的方案也有很多,最普通的就是cookie了,大家也经常都用,但是cookie的缺点是显而易见的,其他的方案比如:IE6以上的user ...
- React 入门之路
React React简介 是由Facebook公司推广的一套框架,已经应用instagram等产品 React就是为了提供应用程序性能而设计的一套框架 在angular中,对dom提供了一些指令,让 ...
- 【spring boot】6.idea下springboot打包成jar包和war包,并且可以在外部tomcat下运行访问到
接着上一章走呗:http://www.cnblogs.com/sxdcgaq8080/p/7712874.html 然后声明一点,下面打包的过程中,scope一直都是使用默认的范围 <!--用于 ...
- ylb:SQL 系统函数
ylbtech-SQL Server: SQL Server-SQL 系统函数 SQL 系统函数 1,ylb:SQL 系统函数 返回顶部 -- ============================ ...
- OpenCV头文件包含问题
opencv从2.2版本以后<opencv root>include下有两个文件夹 opencv 和opencv2.从官方的意思来看,它逐渐喜欢用opencv2里面的那种包含头文件的方式. ...
- 代码篇之AOP框架
AopFrameworkTest类 public class AopFrameworkTest { public static void main(String[] args) throws Exce ...
- mysql 远程登陆不上
当使用 TCP/IP 连接 mysql 时, 出现 : Can't connect to MySQL server on 'xxx.xxx.xxx.xxx.'(111) 这个错误. 经过重复折腾: 确 ...
- 有问必答项目 -数据库设计文档(ask-utf-8)
有问必答项目 -数据库设计文档(ask-utf-8) 表前缀的使用 早期租用公共的服务器 一个数据库,保存多个项目(问答.电子商务.医院),为了区分这些项目,使用前缀分割 ask_ ec_ hospi ...