WPF自定义选择年月控件详解

WPF自定义选择年月控件详解,第1张

概述本文实例为大家分享了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" Render@R_502_4154@Origin="0.5,0.5" Stretch="Fill">                  <Path.Render@R_502_4154@>                    <@R_502_4154@Group>                      <Scale@R_502_4154@/>                      <Skew@R_502_4154@/>                      <Rotate@R_502_4154@ Angle="180"/>                      <Translate@R_502_4154@/>                    </@R_502_4154@Group>                  </Path.Render@R_502_4154@>                </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"></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,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;      }    }  }}

效果图:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

总结

以上是内存溢出为你收集整理的WPF自定义选择年月控件详解全部内容,希望文章能够帮你解决WPF自定义选择年月控件详解所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1253856.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存