2011-12-20 21 views
1

私はTimeを入力するような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 

私はそれが動作しないプロパティやショーの値に別のユーザーコントロールとバインドでこれを使用していますが。 Itemは、データベースとのバインドから読み込まれたエンティティであるとStartTimeはHHMMの短い形式であることを

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

: は、私はこのようにそれを使用します。

+0

を助け

{ // ..... 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システムによって行われてきたとしてあなたはそれに依存している、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; 
    } 
    } 
} 

二つ目は、私は、あなたが間違ったのStartTimeプロパティを使用すると思いますあまりにも依存関係プロパティとしてそれを使用する、またはINotifyPropertyChangedのを実装します。これは

0

getValueとSetValueを依存関係プロパティのgetterとsetter内で呼び出すコードは他にはありません。しかし、これはあなたの問題を解決しないかもしれません。値の変更時にコードを呼び出す場合は、setterの代わりにその内部コールバックメソッドを実行します。

+0

通常、withingというコードは無視され、実行フローには影響しません。 –

関連する問題