Wpf custom datepicker user control

ぐ巨炮叔叔 提交于 2019-12-20 05:55:29

问题


I want to create a user control for getting a date from the user. It should have three textboxes, one for year, month and day. I don't know how to create it.

<UserControl x:Class="UI.WPF.CustomControls.ChooseDateControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         x:Name="chooseDateControl"
         xmlns:custom="clr-namespace:UI.WPF.CustomControls"
         d:DesignHeight="26" d:DesignWidth="181" FontFamily="Tahoma">

<DockPanel LastChildFill="True">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="1.5*" />
            <ColumnDefinition Width="14" />
            <ColumnDefinition Width="1*" />
            <ColumnDefinition Width="14" />
            <ColumnDefinition Width="1*" />
        </Grid.ColumnDefinitions>

        <Label Content="/" Grid.Column="1" />
        <Label Content="/" Grid.Column="3" />

        <custom:NumericTextBox x:Name="txtYear" ToolTip="سال" Text="{Binding ElementName=chooseDateControl, Mode=TwoWay, Path=Year}" MaxLength="4" TabIndex="2" MinWidth="20" />
        <custom:NumericTextBox x:Name="txtMonth" Grid.Column="2" ToolTip="ماه" Text="{Binding ElementName=chooseDateControl, Mode=TwoWay, Path=Month}" MaxLength="2" TabIndex="1" MinWidth="20" />
        <custom:NumericTextBox x:Name="txtDay" Grid.Column="4" ToolTip="روز" Text="{Binding ElementName=chooseDateControl, Mode=TwoWay, Path=Day}" MaxLength="2" TabIndex="0" MinWidth="20" />
    </Grid>
</DockPanel>

Code Behind

public partial class ChooseDateControl : UserControl
{
    public static readonly DependencyProperty ValueProperty;
    public static readonly DependencyProperty YearProperty;
    public static readonly DependencyProperty MonthProperty;
    public static readonly DependencyProperty DayProperty;

    static ChooseDateControl()
    {
        ValueProperty = DependencyProperty.Register(
            "Value",
            typeof(DateTime), typeof(ChooseDateControl),
            new FrameworkPropertyMetadata(DateTime.MinValue));

        ValueProperty = DependencyProperty.Register(
            "Year",
            typeof(int), typeof(ChooseDateControl),
            new FrameworkPropertyMetadata((int)0));

        ValueProperty = DependencyProperty.Register(
            "Month",
            typeof(int), typeof(ChooseDateControl),
            new FrameworkPropertyMetadata((int)0));

        ValueProperty = DependencyProperty.Register(
            "Day",
            typeof(int), typeof(ChooseDateControl),
            new FrameworkPropertyMetadata((int)0));
    }

    public ChooseDateControl()
    {
        InitializeComponent();
    }

    public DateTime Value
    {
        get
        {
            return (DateTime)base.GetValue(ValueProperty);
        }
        set
        {
            base.SetValue(ValueProperty, value);
        }
    }

    public int Year
    {
        get
        {
            return (int)base.GetValue(YearProperty);
        }
        set
        {
            base.SetValue(YearProperty, value);
        }
    }

    public int Month
    {
        get
        {
            return (int)base.GetValue(MonthProperty);
        }
        set
        {
            base.SetValue(MonthProperty, value);
        }
    }

    public int Day
    {
        get
        {
            return (int)base.GetValue(DayProperty);
        }
        set
        {
            base.SetValue(DayProperty, value);
        }
    }
}

It doesn't work correctly-- it returns the default value, which is DateTime.MinValue. Please help me.


回答1:


It might be better writing the logic kind of like this:

static void Main(string[] args)
{
    Console.WriteLine("Year");
    var year = Int32.Parse(Console.ReadLine());
    Console.WriteLine("Month");
    var month = Int32.Parse(Console.ReadLine());
    Console.WriteLine("Day");
    var day = Int32.Parse(Console.ReadLine());

    var customDate = new DateTime(year, month, day);

    Console.WriteLine(customDate);
    Console.ReadLine();
}

You could implement something similar like having a button that when pressed will get the values and create a new datetime value. There are many ways of generating a DateTime from input values just surf the web and you will find a ton of examples and tutorials about this in WPF. Hopefully this will give you some idea's and help you out.




回答2:


Why not just use the Calendar or DatePicker controls in the WPF Toolkit? You haven't mentioned anything in your question that is custom, so this should work ok for you.




回答3:


You don't have your text boxes wired up to your dependency property. The value is never set. You need some type of converter that will convert the values in your text boxes to an actual date too. I think you are better off going with the DatePicker Control in the toolkit (3.5) or the framework (4.0).




回答4:


For starters stop copying and pasting :0). All your DP registrations are for the ValueProperty. And you will have to handle the property changed callback for each property to update the Value property as well.



来源:https://stackoverflow.com/questions/7223345/wpf-custom-datepicker-user-control

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!