這篇文章主要介紹了WPF如何自定義選擇年月控件,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
10余年的環(huán)江網(wǎng)站建設(shè)經(jīng)驗(yàn),針對設(shè)計、前端、開發(fā)、售后、文案、推廣等六對一服務(wù),響應(yīng)快,48小時及時工作處理。營銷型網(wǎng)站 的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動調(diào)整環(huán)江建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計,從而大程度地提升瀏覽體驗(yàn)。創(chuàng)新互聯(lián)從事“環(huán)江網(wǎng)站設(shè)計 ”,“環(huán)江網(wǎng)站推廣 ”以來,每個客戶項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。
具體內(nèi)容如下
封裝了一個選擇年月的控件,XAML代碼:
后臺代碼:
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;
}
}
}
} 效果圖:
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“WPF如何自定義選擇年月控件”這篇文章對大家有幫助,同時也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!
文章標(biāo)題:WPF如何自定義選擇年月控件
瀏覽路徑:
http://weahome.cn/article/ppogjp.html