2017-01-30 32 views
1

我有結合了2個DepedenciyProperties的值的用戶控制內:WPF訪問的DependencyObject一個CollectionChanged事件

INT numberPeople成分列出<>

我想在這些值的更新以重做組合。 我目前的實現使用一個靜態變量來跟蹤對象實例(objectInstance)。我想知道是否有更乾淨的方法來做到這一點。

private static DependencyObject objectInstance; 

    public int numberPeople 
    { 
     get { return (int)GetValue(numberPeopleProperty); } 
     set { SetValue(numberPeopleProperty, value); } 
    } 
    public static readonly DependencyProperty numberPeopleProperty = 
     DependencyProperty.Register("numberPeople", typeof(int), typeof(ListDisplayer), new PropertyMetadata(0, Combine)); 


    public ObservableCollection<ListModel> ingredients 
    { 
     get { return (ObservableCollection<ListModel>)GetValue(ingredientsProperty); } 
     set { SetValue(ingredientsProperty, value); } 
    } 
    public static readonly DependencyProperty ingredientsProperty = 
     DependencyProperty.Register("ingredients", typeof(ObservableCollection<ListModel>), typeof(ListDisplayer), new PropertyMetadata(null, AssignCollectionChangedToList)); 
    private static void AssignCollectionChangedToList(DependencyObject d, DependencyPropertyChangedEventArgs e) 
    { 
     var instance = d as ListDisplayer; 
     if (e.OldValue != null) 
     { 
      var coll = (INotifyCollectionChanged)e.OldValue; 
      coll.CollectionChanged -= ItemsSource_CollectionChanged; 
     } 

     if (e.NewValue != null) 
     { 
      instance.ItemsSource = (ObservableCollection<ListModel>)e.NewValue; 
      objectInstance = instance; 
      instance.ItemsSource.CollectionChanged += ItemsSource_CollectionChanged; 
     } 
    } 

    private static void ItemsSource_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) 
    { 
     // MY PROBLEM: when a new item is added in this list trigger again Combine(), is there 
     // another way to trigger the Combine so that it will process the IngredientList and numberPeople attached to the object ? 
     Combine(objectInstance, new DependencyPropertyChangedEventArgs()); 

    } 




    private static void Combine(DependencyObject d, DependencyPropertyChangedEventArgs e) 
    { 
     // process numberPeople and ingredientList 
    } 

回答

2

編輯:ItemsSource_CollectionChanged事件處理程序的定義中刪除了static關鍵字和使用「實例」引用它掛:

private static void AssignCollectionChangedToList(DependencyObject d, DependencyPropertyChangedEventArgs e) 
{ 
    var instance = d as ListDisplayer; 
    if (e.OldValue != null) 
    { 
     var coll = (INotifyCollectionChanged)e.OldValue; 
     coll.CollectionChanged -= instance.ItemsSource_CollectionChanged; 
    } 

    if (e.NewValue != null) 
    { 
     instance.ingredients = (ObservableCollection<ListModel>)e.NewValue; 
     objectInstance = instance; 
     instance.ingredients.CollectionChanged += instance.ItemsSource_CollectionChanged; 
    } 
} 

private void ItemsSource_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) 
{ 
    Combine(this, new DependencyPropertyChangedEventArgs()); 
} 

而且你的依賴屬性的CLR包裝器沒有正確實施。您應該將依賴項屬性傳遞給GetValue和SetValue方法:

public int numberPeople 
{ 
    get { return (int)GetValue(numberPeopleProperty); } 
    set { SetValue(numberPeopleProperty, value); } 
} 
public static readonly DependencyProperty numberPeopleProperty = 
    DependencyProperty.Register("numberPeople", typeof(int), typeof(ListDisplayer), new PropertyMetadata(0, Combine)); 


public ObservableCollection<ListModel> ingredients 
{ 
    get { return (ObservableCollection<ListModel>)GetValue(ingredientsProperty); } 
    set { SetValue(ingredientsProperty, value); } 
} 
public static readonly DependencyProperty ingredientsProperty = 
    DependencyProperty.Register("ingredients", typeof(ObservableCollection<ListModel>), typeof(ListDisplayer), new PropertyMetadata(null, AssignCollectionChangedToList)); 
+0

更新了代碼以及代碼中的註釋。註釋現在位於方法ItemsSource_CollectionChanged中。希望我已經設法很好地解釋我這個問題是什麼。 –

相關問題