WPF自定義選擇年月控件詳解
本文實(shí)例為大家分享了WPF自定義選擇年月控件的具體代碼,供大家參考,具體內(nèi)容如下
封裝了一個(gè)選擇年月的控件,XAML代碼:
<UserControl x:Class="SunCreate.CombatPlatform.Client.DateMonthPicker" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 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人臉比對(duì)/輸入框.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 用于顯示子級(jí),方法是將 IsItemsHost 設(shè)置為 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>
后臺(tái)代碼:
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; } } } }
效果圖:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:C#強(qiáng)制轉(zhuǎn)換和嘗試轉(zhuǎn)換的方法
欄 目:C#教程
下一篇:C#實(shí)現(xiàn)的簡(jiǎn)單整數(shù)四則運(yùn)算計(jì)算器功能示例
本文標(biāo)題:WPF自定義選擇年月控件詳解
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/5457.html
您可能感興趣的文章
- 01-10C#自定義簽名章實(shí)現(xiàn)方法
- 01-10WinForm實(shí)現(xiàn)自定義右下角提示效果的方法
- 01-10C#實(shí)現(xiàn)自定義windows系統(tǒng)日志的方法
- 01-10C#自定義事件監(jiān)聽實(shí)現(xiàn)方法
- 01-10C#編程實(shí)現(xiàn)自定義熱鍵的方法
- 01-10C#及WPF獲取本機(jī)所有字體和顏色的方法
- 01-10WPF實(shí)現(xiàn)類似360安全衛(wèi)士界面的程序源碼分享
- 01-10WPF實(shí)現(xiàn)時(shí)鐘特效
- 01-10超炫酷的WPF實(shí)現(xiàn)Loading控件效果
- 01-10輕松學(xué)習(xí)C#的方法


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10C#通過(guò)反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁(yè)無(wú)法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過(guò)重寫Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-10C#中split用法實(shí)例總結(jié)
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 04-02jquery與jsp,用jquery
- 01-10delphi制作wav文件的方法