簡體   English   中英

WPF自定義日期選擇器用戶控件

[英]Wpf custom datepicker user control

我想創建一個用於從用戶那里獲取日期的用戶控件。 它應該有三個文本框,一個分別用於年,月和日。 我不知道如何創建它。

<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。 請幫我。

編寫這樣的邏輯可能會更好:

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值。 只需在Web上瀏覽,就可以通過許多輸入值生成DateTime的方法,您將在WPF中找到大量與此相關的示例和教程。 希望這會給您一些想法並幫助您。

為什么不只使用WPF工具包中CalendarDatePicker控件? 您沒有在問題中提及任何自定義問題,因此對您來說應該可以。

您沒有將文本框連接到依賴項屬性。 該值從不設置。 您還需要某種類型的轉換器,也可以將文本框中的值轉換為實際日期。 我認為最好使用工具包(3.5)或框架(4.0)中的DatePicker控件。

對於初學者,請停止復制和粘貼:0)。 您所有的DP注冊都針對ValueProperty 而且,您還必須處理每個屬性的屬性更改回調,以更新Value屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM