0

ユーザーから日付を取得するためのユーザー コントロールを作成したいと考えています。年、月、日の 3 つのテキスト ボックスが必要です。作成方法がわかりません。

<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

4 に答える 4

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

ボタンを押すと値を取得し、新しい日時値を作成するようなものを実装できます。Web をサーフィンするだけで、入力値から DateTime を生成する方法はたくさんあります。WPF には、これに関する例やチュートリアルがたくさんあります。うまくいけば、これはあなたにいくつかのアイデアを与え、あなたを助けるでしょう.

于 2011-08-29T15:48:30.397 に答える
0

手始めに、コピーと貼り付けを停止します:0)。すべてのDP登録はのためのものValuePropertyです。また、Valueプロパティも更新するには、各プロパティのプロパティ変更コールバックを処理する必要があります。

于 2011-08-29T13:34:52.427 に答える
0

WPF ToolkitCalendarでまたはDatePickerコントロールを使用しないのはなぜですか? 質問でカスタムのことについて何も言及していないので、これで問題なく機能するはずです。

于 2011-08-28T19:33:21.807 に答える
0

依存関係プロパティに接続されたテキスト ボックスがありません。値は設定されません。テキストボックスの値を実際の日付に変換するコンバーターも必要です。DatePickerツールキット(3.5)またはフレームワーク(4.0)のコントロールを使用する方がよいと思います。

于 2011-08-29T03:53:07.957 に答える