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

本文共 4786 字,大约阅读时间需要 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

效果图:

 

posted on
2019-04-02 11:57 阅读(
...) 评论(
...)

转载于:https://www.cnblogs.com/lonelyxmas/p/10641795.html

你可能感兴趣的文章
比callback更简洁的链式执行promise
查看>>
android permission
查看>>
【译】在Asp.Net中操作PDF - iTextSharp - 使用字体
查看>>
.net 文本框只允许输入XX,(正则表达式)
查看>>
实验2-2
查看>>
MongoDB遇到的疑似数据丢失的问题。不要用InsertMany!
查看>>
android smack MultiUserChat.getHostedRooms( NullPointerException)
查看>>
IOS Google语音识别更新啦!!!
查看>>
[置顶] Linux终端中使用上一命令减少键盘输入
查看>>
BootScrap
查看>>
Java实现二分查找
查看>>
UIImage 和 iOS 图片压缩UIImage / UIImageVIew
查看>>
php7 新特性整理
查看>>
RabbitMQ、Redis、Memcache、SQLAlchemy
查看>>
03 线程池
查看>>
手机验证码执行流程
查看>>
设计模式课程 设计模式精讲 2-2 UML类图讲解
查看>>
Silverlight 的菜单控件。(不是 Toolkit的)
查看>>
jquery的contains方法
查看>>
linux后台运行和关闭SSH运行,查看后台任务
查看>>