Custom Control .DateTimePicker_01

1, environment: vs2017x64

2, create a project:

 2.1, ZC: Here To select the "Windows Desktop" on the left, or can not find "WPF Custom Control Library (.NET Framework)" [WPF Custom Control Library (not "User Control Library")] on the right side

  ZC: I remember the native WPF Controls "Calendar" .net version is required, looks like .net4.0 have? ? (Can not remember)

  

 

  2.2, the original "CustomControl1.cs" renamed "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 (note that it is a parent Control, rather than the 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, other documents, are being held in default of the original look

  To create a project here, is over

 

3、

4、

5、

6、

7、

 

Guess you like

Origin www.cnblogs.com/csskill/p/11574969.html