2009-04-02 7 views
3

개체의 DateTime 값을 편집하는 것을 포함하는 GUI를 작성하려고합니다. DateTime 속성은 DataPicker 및 시간에 대한 일반 TextBox에 바인딩됩니다. Time TextBox의 값을 변경하면 DateTime 속성의 값은 시간이 업데이트 된 대신 입력 한 시간으로 변경됩니다. 원래 날짜는 그대로 유지됩니다.DateTime을 날짜와 시간으로 바인딩하기

어떻게 DateTime 시간 만 변경하고 날짜는 변경하지 않는 Time TextBox를 구현할 수 있습니까?

현재 바인딩 : 내가 할 생각할 수있는

<TextBlock>Time</TextBlock> 
<TextBox Template="{StaticResource RoundedTextBoxTemplate}"> 
    <Binding Path="Delivery.Date" StringFormat="HH:mm"> 
     <Binding.ValidationRules> 
      <v:IsValidTimeRule /> 
     </Binding.ValidationRules> 
    </Binding> 
</TextBox> 

답변

4

유일한 방법은 당신 만 세터가 시간을 변경할 수 있도록 날짜 시간 속성을 가지고있다.

XAML :

<UserControl.Resources> 
    <mine:DateTimeConverter x:Key="MyDateTimeConverter" /> 
</UserControl.Resources>  
<Grid x:Name="LayoutRoot"> 
    <TextBox x:Name="myTextBox" Text="{Binding Path=TestDateTime, Converter={StaticResource MyDateTimeConverter}, Mode=TwoWay}" /> 
</Grid> 

C# 코드 숨김

public partial class Page : UserControl 
{ 
     private TestClass m_testClass = new TestClass(); 

     public Page() 
     { 
      InitializeComponent(); 
      myTextBox.DataContext = m_testClass; 
     } 
    } 

C# 속성 세터 제한 사용의 TestClass :

public class TestClass : INotifyPropertyChanged 
{ 
    private DateTime m_testDateTime = DateTime.Now; 
    public DateTime TestDateTime 
    { 
     get { return m_testDateTime; } 
     set 
     { 
      m_testDateTime = m_testDateTime.Date.Add(value.TimeOfDay); 
      PropertyChanged(this, new PropertyChangedEventArgs("TestDateTime")); 
     } 
    } 

    public event PropertyChangedEventHandler PropertyChanged = (t, e) => {}; 
} 

C#을 IValueConverter :

public class DateTimeConverter : IValueConverter 
{ 
    public object Convert(object value, 
         Type targetType, 
         object parameter, 
         CultureInfo culture) 
    { 
     DateTime date = (DateTime)value; 
     return date.ToString("HH:mm"); 
    } 

    public object ConvertBack(object value, 
           Type targetType, 
           object parameter, 
           CultureInfo culture) 
    { 
     string strValue = value.ToString(); 
     DateTime resultDateTime; 
     if (DateTime.TryParse(strValue, out resultDateTime)) 
     { 
      return resultDateTime; 
     } 
     return value; 
    } 
} 
2

나는 sipWiz의 솔루션을 조금 확장하지만 난 두 필드 모두 장면 behid 내 기본 필드 StartDate에 액세스 StartDay 2. StartTime 1. 생성

내 모델 클래스에 모두 떠났다.

public DateTime? StartTime 
{ 
    get 
    { 
     return StartDate; 
    } 
    set 
    { 
     if (StartDate == null) StartDate = DateTime.Today; 
     StartDate = StartDate.Value.Date.Add(value.HasValue ? value.Value.TimeOfDay: TimeSpan.Zero); 
    } 
} 
public DateTime? StartDay 
{ 
    get { return StartDate; } 
    set 
    { 
     DateTime BaseDate = value.HasValue ? value.Value : DateTime.MinValue; 
     StartDate = BaseDate.Date.Add(StartDate.HasValue ? StartDate.Value.TimeOfDay : TimeSpan.Zero); 
    } 
} 

그럼 당신은있는 StringFormat = ": mm HH"로 시간 필드에 하루 필드에 DatePicker에서와 텍스트 상자를 바인딩 할 수 있습니다.

관련 문제