WPF自定义选择年月控件详解
作者:秋荷雨翔 发布时间:2021-09-05 23:48:34
标签:WPF,年月控件
本文实例为大家分享了WPF自定义选择年月控件的具体代码,供大家参考,具体内容如下
封装了一个选择年月的控件,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人脸比对/输入框.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>
后台代码:
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;
}
}
}
}
效果图:
来源:http://www.cnblogs.com/s0611163/p/7573774.html


猜你喜欢
- 本文实例讲述了Android编程实现将应用强制装到手机内存的方法。分享给大家供大家参考,具体如下:在Froyo(android 2.2,AP
- 如下所示:import java.util.*;public class Main{public static void main(Stri
- 为什么需要协程?协程可以简化异步编程,可以顺序地表达程序,协程也提供了一种避免阻塞线程并用更廉价、更可控的操作替代线程阻塞的方法 &
- 1.简介本教程将介绍如何在Spring Security中设置身份验证提供程序,与使用简单UserDetailsService的标准方案相比
- 本文实例讲述了C#调用SQLite的方法。分享给大家供大家参考。具体分析如下:一、SQLite简介:当我们用到海量数据时一般会用Oracle
- 一:ArrayList和LinkedList的大致区别如下:1.ArrayList是实现了基于动态数组的数据结构,ArrayList实现了长
- hadoop做的一个简单grep程序,可从文档中提取包含某些字符串的行/* * 一个简单grep程序,可从文档中提取包含莫些字符串
- 1. 概述当一个对象的内在状态改变时允许改变其行为,这个对象看起来像是改变了其类。2. 解决的问题主要解决的是当控制一个对象状态转换的条件表
- 设计算法,计算两给定基因序列的相似程度。人类基因由4种核苷酸,分别用字母ACTG表示。要求编写一个程序,按以下规则比较两个基因序列并确定它们
- 到底什么是反射呢???反射的核心就是JVM在运行时才动态加载类或调用方法,访问属性,它不需要事先(写代码的时候或编译期)知道运行对象是谁。每
- 一、背景日常开发中,有时候需要根据某个 key 加锁,确保多线程情况下,对该 key 的加锁和解锁之间的代码串行执行。大家可以借助每个 ke
- 最近上线的项目中数据库数据已经临近饱和,最大的一张表数据已经接近3000W,百万数据的表也有几张,项目要求读数据(select)时间不能超过
- java 多线程的三种构建方法继承Thread类创建线程类public class Thread extends Object
- android多线程断点下载,带进度条和百分比显示,断点下载的临时数据保存到SD卡的文本文档中,建议可以保存到本地数据库中,这样可以提高存取
- 什么是Run Dashboard当springcloud的服务有多个时,管理多个服务的启动使用run会不好管理,这样我们就可以使用Run D
- 一 前言redis在分布式应用十分广泛,本篇文章也是互联网面试的重点内容,读者至少需要知道为什么需要分布式锁,分布式锁的实现原理,分布式锁的
- 一个应用场景,浏览器上传一个文件,此文件后台调用文件转换,需要耗费相当长的时间,这样,如果是一个线程同步式的做下去,那么用户在浏览器上感觉就
- C#中打印其实就是自己绘图+调用系统打印函数,于是便有了以下操作1.调用打印机设置如果你想在打印前设置打印机属性(或者切换打印机),请务必添
- 仅供学习交流,禁止商业用途。如侵害利益,联系必删!前言最近一位小伙伴钟爱二次元文化,于是找到半次元这个app,但是很快他就遇到了问题。一、案
- 原装的Android标题栏配色比较单调,就是黑色的一坨,现在假设你的软件需要独自添加标题栏,这样不仅美观而且可以将进度条等加进去,如何实现: