Wpf自定义datepicker用户控件

时间:2011-08-28 19:27:23

标签: c# wpf xaml user-controls xaml-binding

我想创建一个用户控件来获取用户的日期。它应该有三个文本框,一个用于年,月和日。我不知道如何创建它。

<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>

代码背后

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);
        }
    }
}

它无法正常工作 - 它返回默认值,即DateTime.MinValue。请帮帮我。

4 个答案:

答案 0 :(得分:1)

写这样的逻辑可能更好:

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();
}

你可以实现类似的东西,比如有一个按钮,当按下它时会得到值并创建一个新的日期时间值。有许多方法可以从输入值生成DateTime,只需浏览网页,您就可以在WPF中找到大量关于此内容的示例和教程。希望这会给你一些想法并帮助你。

答案 1 :(得分:0)

为什么不使用WPF Toolkit中的CalendarDatePicker控件?你没有在你的问题中提到任何自定义的东西,所以这应该对你有效。

答案 2 :(得分:0)

您没有将文本框连接到您的依赖项属性。永远不会设置该值。您需要某种类型的转换器,它会将文本框中的值转换为实际日期。我认为你最好使用工具包(3.5)或框架(4.0)中的DatePicker控件。

答案 3 :(得分:0)

首先停止复制和粘贴:0)。您的所有DP注册均适用于ValueProperty。并且您将必须处理每个属性的属性更改回调以更新Value属性。