2011-08-28 40 views
0

我想創建一個用戶控件來獲取用戶的日期。它應該有三個文本框,一個用於年,月和日。我不知道如何創建它。Wpf自定義日期選擇器用戶控件

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

它不工作correctly--它返回默認值,這是DateTime.MinValue。請幫幫我。

+0

你仍然需要修復你的財產註冊。您正在將所有內容註冊到ValueProperty。請確保您更改爲設置YearProperty,MonthyProperty等...... –

+1

設置值的邏輯還存在一些問題,對於您從不設置Value屬性的問題。您可能需要查看擴展WPF工具包中的DatTimeUpDown控件,以瞭解它在此處的處理方式:http://wpftoolkit.codeplex.com/ –

+1

我假設您要使用Value屬性作爲用於數據綁定。這意味着你將不得不處理屬性changted call basck中的Value屬性來設置其他屬性值。在其他的回調函數中,您可能需要使用新的D/M/Y組合來設置Value屬性。 –

回答

0

爲什麼不使用WPF Toolkit中的CalendarDatePicker控件?您在自己的問題中沒有提及任何自定義的問題,因此這應該對您有用。

0

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

+0

謝謝你的回答 - 因爲datepicker沒有波斯日期和其他原因。請幫忙。我編輯的代碼,請再次看到它 –

+0

那麼現在更新的代碼會發生什麼?你是否通過調試器進入控制系統?你的屬性是否被設置?您還可以使用Snoop查看是否有任何綁定錯誤,並查看數據上下文的設置。 – tsells

+0

結果是DateTime.MinValue –

0

對於初學者停止複製和粘貼:0)。所有您的DP註冊都是爲ValueProperty。而且你必須處理每個屬性的屬性更改回調以更新Value屬性。

+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中找到大量關於此的示例和教程。希望這會給你一些想法並幫助你。