博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
WPF自定义控件
阅读量:6585 次
发布时间:2019-06-24

本文共 4671 字,大约阅读时间需要 15 分钟。

封装了一个选择年月的控件,XAML代码:

View Code

后台代码:

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{    ///     ///     ///     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 yearList = new List();            for (int i = currentYear - 20; i <= currentYear; i++)            {                yearList.Add(new { Text = i.ToString() });            }            cbYear.ItemsSource = yearList;            cbMonth.ItemsSource = new List() {                 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;            }        }    }}
View Code

效果图:

 

转载于:https://www.cnblogs.com/s0611163/p/7573774.html

你可能感兴趣的文章
只需三步轻松搞定 Foxmail 发送邮件“错误信息 ssl连接错误 error code 5”
查看>>
使用openssl加密文件
查看>>
启动ssh服务报错
查看>>
AIX系统中如何查看HBA卡的WWPN和微码版本
查看>>
check the manual that corresponds to your MySQL server version for the right syntax to use near
查看>>
spring创建连接池的几种方式
查看>>
在JSTL EL中处理java.util.Map,及嵌套List集合
查看>>
我的友情链接
查看>>
使用shell解决 年月日目录分级结构
查看>>
offsetTop、offsetLeft、offsetWidth、offsetHeight
查看>>
java基础知识要点(二)
查看>>
LAMP之网站搭建(二)
查看>>
nginx-负载均衡
查看>>
linux学习计划
查看>>
GCE 部署 ELK 7.1可视化分析 nginx
查看>>
Rancher2.0中邮件通知的设置
查看>>
OSI七层参考模型-数据链路层
查看>>
华为P30发布,10秒2个亿销售额,这项技术升级是重点!
查看>>
poj 1155
查看>>
JS-cookie封装
查看>>