2013-01-31 3 views
2

C# 4.0 (WPF) 응용 프로그램에 datepicker가 있는데 yyyy/MM/dd에 textBox에 표시되는 날짜 형식을 변경하고 싶습니다. 이제 dd/MM/yyyy 형식을 봅니다. 날짜 선택기의 내 axml에서C# 및 날짜 선택 도구, 형식을 변경하는 방법은 무엇입니까?

이 코드가 있습니다

이 모두 잘 작동하는 처음에 보인다
<DatePicker Height="25" HorizontalAlignment="Left" Margin="5,36,0,0" Name="dtpStartDate" 
        SelectedDate="{Binding StartDateSelectedDate}" VerticalAlignment="Top" Width="115"> 
      <DatePicker.Resources> 
       <Style TargetType="{x:Type DatePickerTextBox}"> 
        <Setter Property="Control.Template"> 
         <Setter.Value> 
          <ControlTemplate> 
           <TextBox x:Name="PART_TextBox" 
            Text="{Binding Path=SelectedDate, RelativeSource={RelativeSource AncestorType={x:Type DatePicker}}, StringFormat={}{0:yyyy/MM/dd}}" /> 
          </ControlTemplate> 
         </Setter.Value> 
        </Setter> 
       </Style> 
      </DatePicker.Resources> 
     </DatePicker> 

, 나는 내가 원하는 형식으로 날짜를 볼 수 있고, 내가 바꿀 수를 날짜를 수동으로 또는 달력을 사용하여 가져오고 두 가지 방법으로 viewModel에 도착한 날짜가 올바른지 확인합니다.

그러나 내보기 모델 컨트롤에서이 경우 날짜가 비어있는 경우이를 감지하고 싶기 때문에 문제가 있습니다. 그러나 datepicker를 지우면 내 뷰 모델에 마지막으로 올바른 날짜가 도착하므로 날짜가 비어 있는지 확인할 수 없습니다.

그렇다면 날짜 선택기 및 컨트롤에서 날짜 형식을 수정하여 날짜가 비 었는지 여부를 어떻게 알 수 있습니까?

감사합니다. Daimroc.

답변

5

다음 해결책을 시도해 볼 수 있습니다.

먼저 다음 변환 만들기 : XAML에서 그런

public class StringToDateTimeConverter : IValueConverter 
{ 

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     if (value == null) 
     { 
      return null; 
     } 
     return ((DateTime)value).ToString(parameter as string, CultureInfo.InvariantCulture); 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     if (string.IsNullOrEmpty(value as string)) 
     { 
      return null; 
     } 
     try 
     { 
      DateTime dt = DateTime.ParseExact(value as string, parameter as string, CultureInfo.InvariantCulture); 
      return dt as DateTime?; 
     } 
     catch (Exception) 
     { 
      return null; 
     } 
    } 
} 

을, 당신은 컨버터의 인스턴스를 생성하고 DatePicker에서

<Window x:Class="TestDatePicker.MainWindow" 
    ... 
    xmlns:converters="clr-namespace:TestDatePicker" 
    Title="MainWindow" Height="350" Width="525"> 
<Window.Resources> 
    ... 
    <converters:StringToDateTimeConverter x:Key="StringToDateTimeConverter" /> 
</Window.Resources> 
<Grid DataContext="{StaticResource MainWindowVM}"> 
    ... 
    <DatePicker Height="25" HorizontalAlignment="Left" Margin="5,36,0,0" Name="dtpStartDate" 
       SelectedDate="{Binding StartDateSelectedDate}" VerticalAlignment="Top" Width="115"> 
     <DatePicker.Resources> 
      <Style TargetType="{x:Type DatePickerTextBox}"> 
       <Setter Property="Control.Template"> 
        <Setter.Value> 
         <ControlTemplate> 
          <TextBox x:Name="PART_TextBox" 
           Text="{Binding Path=SelectedDate, RelativeSource={RelativeSource AncestorType={x:Type DatePicker}}, Converter={StaticResource StringToDateTimeConverter}, ConverterParameter=yyyy/MM/dd}" /> 
         </ControlTemplate> 
        </Setter.Value> 
       </Setter> 
      </Style> 
     </DatePicker.Resources> 
    </DatePicker> 
    ... 
</Grid> 

의 텍스트 상자를 사용해야합니다

마지막으로 viewmodel에서 속성의 형식은 DateTime?이어야합니다. (nullable DateTime).

private DateTime? _startDateSelectedDate; 
    public DateTime? StartDateSelectedDate 
    { 
     get { return _startDateSelectedDate; } 
     set 
     { 
      if (_startDateSelectedDate != value) 
      { 
       _startDateSelectedDate = value; 
       RaisePropertyChanged(() => this.StartDateSelectedDate); 
      } 
     } 
    } 

나는이, StartDateSelectedDate 속성은 사용자 정의 형식을 얻을 사용해야 시스템 날짜 형식을 사용하는 뷰 모델의 StartDateSelectedDate 속성에 당신

감사

클로드

+0

하지만 도움이되기를 바랍니다. 내 컴퓨터 형식이 "dd/MM/yyyy"이고 viewmodel 속성에서 "MM/dd/yyyy"를 원할 때? –

2

기본적으로 DateTimerPicker는 null 값을 지원하지 않습니다.

아마 같은 주제의 MSDN에서 this post을 (를) 도울 수 있습니다.

여기에는 구현할 수있는 다른 방법이나 null 입력 가능 날짜 시간 선택기에 대한 코드 프로젝트가 있습니다.

관련 문제