自定义控件.DateTimePicker_01

1、环境:vs2017x64

2、创建项目:

 2.1、ZC:这里 要在左侧选择"Windows Desktop",不然在右侧找不到 "WPF Custom Control Library(.NET Framework)"[ WPF自定义控件库(不是 "用户控件库") ]

  ZC:我记得 WPF原生控件 "Calendar" 是有 .net版本要求的,貌似是 .net4.0才有 ??(记不清)

  

  2.2、将原来的 "CustomControl1.cs" 改名为 "DateTimePickerZ.cs"

   

  2.3、Generic.xaml

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfCustomControlLibrary_DateTimePicker">
    <Style TargetType="{x:Type local:DateTimePickerZ}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:DateTimePickerZ}">
                    <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">

                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

 2.4、DateTimePickerZ.cs (注意它的父类是 Control,而非 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;

namespace WpfCustomControlLibrary_DateTimePicker
{
    /// <summary>
    /// Follow steps 1a or 1b and then 2 to use this custom control in a XAML file.
    ///
    /// Step 1a) Using this custom control in a XAML file that exists in the current project.
    /// Add this XmlNamespace attribute to the root element of the markup file where it is 
    /// to be used:
    ///
    ///     xmlns:MyNamespace="clr-namespace:WpfCustomControlLibrary_DateTimePicker"
    ///
    ///
    /// Step 1b) Using this custom control in a XAML file that exists in a different project.
    /// Add this XmlNamespace attribute to the root element of the markup file where it is 
    /// to be used:
    ///
    ///     xmlns:MyNamespace="clr-namespace:WpfCustomControlLibrary_DateTimePicker;assembly=WpfCustomControlLibrary_DateTimePicker"
    ///
    /// You will also need to add a project reference from the project where the XAML file lives
    /// to this project and Rebuild to avoid compilation errors:
    ///
    ///     Right click on the target project in the Solution Explorer and
    ///     "Add Reference"->"Projects"->[Select this project]
    ///
    ///
    /// Step 2)
    /// Go ahead and use your control in the XAML file.
    ///
    ///     <MyNamespace:CustomControl1/>
    ///
    /// </summary>
    public class DateTimePickerZ : Control
    {
        static DateTimePickerZ()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(DateTimePickerZ), new FrameworkPropertyMetadata(typeof(DateTimePickerZ)));
        }
    }
}

 2.5、其他文件,暂时都是保持的默认的原来的样子

  到这里 创建工程,就结束了

3、

4、

5、

6、

7、

猜你喜欢

转载自www.cnblogs.com/csskill/p/11574969.html
今日推荐