2011-12-20 3 views
1

I 입력 시간과 마찬가지로 TimeInput 컨트롤을 만듭니다.UserControl의 DependencyProperty에 바인딩

<TextBox Text="{Binding Path=Hours}" /> 
<TextBox IsReadOnly="True" 
     Focusable="False" 
     Text=":" /> 
<TextBox Text="{Binding Path=Minutes}" /> 

public int Hours { 
    get { return (int)this.GetValue(HoursProperty); } 
    set { 
    this.SetValue(HoursProperty, value); 
    this.OnPropertyChanged("Hours"); 
    } 
} 

public static readonly DependencyProperty HoursProperty = 
    DependencyProperty.Register("Hours", typeof(int), typeof(UserControl1), new UIPropertyMetadata(0, new PropertyChangedCallback(OnHoursChanged))); 

private static void OnHoursChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e) { 
    if (obj != null) { 
    int newValue = (int)e.NewValue; 
    } 
} 

public int Minutes { 
    get { return (int)this.GetValue(MinutesProperty); } 
    set { 
    this.SetValue(MinutesProperty, value); 
    this.OnPropertyChanged("Minutes"); 
    } 
} 

// Using a DependencyProperty as the backing store for Minutes. This enables animation, styling, binding, etc... 
public static readonly DependencyProperty MinutesProperty = 
    DependencyProperty.Register("Minutes", typeof(int), typeof(UserControl1), new UIPropertyMetadata(0, new PropertyChangedCallback(OnMinutesChanged))); 

private static void OnMinutesChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e) { 
    if (obj != null) { 
    int newValue = (int)e.NewValue; 
    } 
} 

public Nullable<TimeSpan> Value { 
    get { return (Nullable<TimeSpan>)this.GetValue(ValueProperty); } 
    set { 
    this.SetValue(ValueProperty, value); 
    this.OnPropertyChanged("Value"); 
    } 
} 

// Using a DependencyProperty as the backing store for Value. This enables animation, styling, binding, etc... 
public static readonly DependencyProperty ValueProperty = 
    DependencyProperty.Register("Value", typeof(Nullable<TimeSpan>), typeof(UserControl1), new UIPropertyMetadata(null, new PropertyChangedCallback(OnValueChanged))); 

private static void OnValueChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e) { 
    if (obj != null) { 
    (obj as UserControl1).UpdateTime(e.NewValue as TimeSpan?); 
    } 
} 

public void UpdateTime(TimeSpan? newTimeSpan) { 
    if (newTimeSpan.HasValue) { 
    this.Hours = newTimeSpan.Value.Hours; 
    this.Minutes = newTimeSpan.Value.Minutes; 
    } 
} 

#region INotifyPropertyChanged Members 

public event PropertyChangedEventHandler PropertyChanged; 

protected void OnPropertyChanged(string name) { 
    PropertyChangedEventHandler handler = this.PropertyChanged; 
    if (handler != null) { 
    handler(this, new PropertyChangedEventArgs(name)); 
    } 
} 

#endregion 

내가 속성에 다른 UserControl을하고 바인딩에서이를 사용하지만 그것은 작동하고 값 표시하지 않습니다. 나는 이런 식으로 그것을 사용 : Item 데이터베이스와 결합하고 StartTime에서 읽을 엔티티가 있음을

<uc:UserControl1 Value="{Binding StartTime}"/> 

public TimeSpan StartTime 
{ 
    get { return new Types.Time(Item.StartTime).ToTimeSpan(); } 
    set { Item.StartTime = new Types.Time(value).ToShort(); NotifyPropertyChanged("StartTime"); } 
} 

은 HHMM의 짧은 형태이다.

+0

당신은 단지에 의존에서 INotifyPropertyChanged를 필요로하지 않는 데 도움이됩니다. –

답변

0

코드를 업데이트했으며 속성 변경 이벤트를 발생시키지 않아도되는 종속성 속성을 명시 적으로 적용했습니다.

public partial class UserControl1 : UserControl 
{ 
    public UserControl1() { 
    this.InitializeComponent(); 
    } 

    public int Hours { 
    get { return (int)this.GetValue(HoursProperty); } 
    set { this.SetValue(HoursProperty, value); } 
    } 

    public static readonly DependencyProperty HoursProperty = 
    DependencyProperty.Register("Hours", typeof(int), typeof(UserControl1), new UIPropertyMetadata(0, new PropertyChangedCallback(OnHoursChanged))); 

    private static void OnHoursChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e) { 
    var uc = obj as UserControl1; 
    if (uc != null && e.NewValue != e.OldValue) { 
     int newValue = (int)e.NewValue; 
     uc.TimeValue = new TimeSpan(newValue, uc.Minutes, 0); 
    } 
    } 

    public int Minutes { 
    get { return (int)this.GetValue(MinutesProperty); } 
    set { this.SetValue(MinutesProperty, value); } 
    } 

    // Using a DependencyProperty as the backing store for Minutes. This enables animation, styling, binding, etc... 
    public static readonly DependencyProperty MinutesProperty = 
    DependencyProperty.Register("Minutes", typeof(int), typeof(UserControl1), new UIPropertyMetadata(0, new PropertyChangedCallback(OnMinutesChanged))); 

    private static void OnMinutesChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e) { 
    var uc = obj as UserControl1; 
    if (uc != null && e.NewValue != e.OldValue) { 
     int newValue = (int)e.NewValue; 
     uc.TimeValue = new TimeSpan(uc.Hours, newValue, 0); 
    } 
    } 

    public Nullable<TimeSpan> TimeValue { 
    get { return (Nullable<TimeSpan>)this.GetValue(ValueProperty); } 
    set { this.SetValue(ValueProperty, value); } 
    } 

    public static readonly DependencyProperty ValueProperty = 
    DependencyProperty.Register("TimeValue", typeof(Nullable<TimeSpan>), typeof(UserControl1), new UIPropertyMetadata(null, new PropertyChangedCallback(OnValueChanged))); 

    private static void OnValueChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e) { 
    var uc = obj as UserControl1; 
    if (uc != null && e.NewValue != e.OldValue) { 
     uc.UpdateTime(e.NewValue as TimeSpan?); 
    } 
    } 

    public void UpdateTime(TimeSpan? newTimeSpan) { 
    if (newTimeSpan.HasValue) { 
     this.Hours = newTimeSpan.Value.Hours; 
     this.Minutes = newTimeSpan.Value.Minutes; 
    } 
    } 
} 

초, 난, 당신이 올바르지 상영 속성을 사용할 생각도 종속성 속성으로 사용하거나에서 INotifyPropertyChanged를 구현합니다.

{ 
    // ..... 
    StartTime = new Types.Time(this.Item.StartTime).ToTimeSpan(); 
    // ..... 
} 


public static readonly DependencyProperty StartTimeProperty = 
    DependencyProperty.Register("StartTime", typeof(TimeSpan?), typeof(Window1), new PropertyMetadata(default(TimeSpan?), new PropertyChangedCallback(OnStartTimePropertyChanged))); 

private static void OnStartTimePropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e) { 
    if(e.NewValue != e.OldValue) { 
    (dependencyObject as Window1).Item.StartTime = new Types.Time(e.NewValue).ToShort(); 
    } 
} 

public TimeSpan? StartTime { 
    get { return (TimeSpan?)GetValue(StartTimeProperty); } 
    set { SetValue(StartTimeProperty, value); } 
} 

희망이는 이미 DP 시스템에 의해 수행되었습니다로

0

GetValue 및 SetValue를 getter 및 dependter 속성의 setter 내부에서 호출하는 다른 코드는 없어야합니다. 그러나 이것으로 문제가 해결되지 않을 수도 있습니다. 값을 변경할 때 코드를 호출하려면 setter 대신 내부 콜백 메소드를 수행하십시오.

+0

일반적으로 withing 코드는 무시되고 실행 흐름에 아무런 영향을주지 않습니다. –

관련 문제