2011-08-28 8 views
0

사용자로부터 날짜를 가져 오는 사용자 정의 컨트롤을 만들고 싶습니다. 그것은 세 개의 텍스트 상자가 있어야합니다. 하나는 년, 월, 일입니다. 나는 그것을 만드는 법을 모른다. 이 DateTime.MinValue 인 디폴트 값을 반환 correctly--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); 
     } 
    } 
} 

뒤에 그것은 작동하지 않습니다. 도와주세요.

+0

재산 등록을 수정해야합니다. 모든 것을 ValueProperty에 등록합니다. YearProperty, MonthyProperty 등을 설정하도록 변경하십시오. –

+1

값을 설정하는 논리에는 몇 가지 문제가 있습니다. 하나는 Value 속성을 설정하지 않습니다. 확장 WPF 툴킷의 DatTimeUpDown 컨트롤을보고 어떻게 처리되는지 확인할 수 있습니다. http://wpftoolkit.codeplex.com/ –

+1

Value 속성을 사용하는 속성으로 사용하려는 경우 데이터 바인딩. 즉, 다른 속성 값을 설정하기 위해 Value 속성의 변경된 호출 basck 속성을 처리해야합니다. 다른 속성 콜백에서는 새 D/M/Y 조합을 사용하여 Value 속성을 설정하려고 할 것입니다. –

답변

0

WPF Toolkit에서 Calendar 또는 DatePicker 개의 컨트롤을 사용하지 않는 이유는 무엇입니까? 질문에 사용자 정의 항목을 언급하지 않았으므로이 기능이 정상적으로 작동합니다.

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에서 이에 대한 많은 예제와 튜토리얼을 찾을 수 있습니다. 다행히도 이것은 당신에게 아이디어를주고 당신을 도울 것입니다.

관련 문제