2014-03-02 145 views
1

我有以下類別gist with the classes
我想將Item.Visible綁定到Items.ItemsVisible--是否有可能?如果是這樣 - 如何?將一個類別的值綁定到另一個類別的另一個值

Item.cs:

using System; 
using System.ComponentModel; 

namespace WpfApplication85 
{ 
    /// <summary> 
    /// Item Object. 
    /// </summary> 
    public class Item : INotifyPropertyChanged 
    { 
     #region INotifyPropertyChanged 

     public event PropertyChangedEventHandler PropertyChanged; //Event to notify when Property changed. 

     /// <summary> 
     /// Notify that Property has Changed. 
     /// </summary> 
     /// <param name="propertyName">The name of the Property</param> 
     protected void NotifyPropertyChanged(string propertyName) 
     { 
      if (PropertyChanged != null) 
      { 
       PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
      } 
     } 

     #endregion 

     #region Private Variables 

     private bool _Visible; //Bool to determine if the Item is visible or not 

     #endregion 

     #region Public Properties 

     //Return the value of Visible/Set the value of Visible and Notify. 
     public bool Visible 
     { 
      get { return _Visible; } 
      set 
      { 
       _Visible = value; 
       NotifyPropertyChanged("Visible"); 
      } 
     } 

     #endregion 

     #region Constructor 

     /// <summary> 
     /// Item Constructor 
     /// </summary> 
     public Item() 
     { 
      _Visible = true; 
     } 

     #endregion 
    } 
} 

Items.cs:

using System; 
using System.Collections.ObjectModel; 
using System.ComponentModel; 

namespace WpfApplication85 
{ 
    /// <summary> 
    /// Items Object. 
    /// </summary> 
    public class Items : INotifyPropertyChanged 
    { 
     #region INotifyPropertyChanged 

     public event PropertyChangedEventHandler PropertyChanged; //Event to notify when Property changed. 

     /// <summary> 
     /// Notify that Property has Changed. 
     /// </summary> 
     /// <param name="propertyName">The name of the Property</param> 
     protected void NotifyPropertyChanged(string propertyName) 
     { 
      if (PropertyChanged != null) 
      { 
       PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
      } 
     } 

     #endregion 

     #region Private Variables 

     private bool _itemsVisible; //Bool to determine if the Items are visible or not 
     private ObservableCollection<Item> _itemsCollection; //Collection of Items. 

     #endregion 

     #region Public Properties 

     //Return the value of ItemsVisible/Set the value of ItemsVisible and Notify. 
     public bool ItemsVisible 
     { 
      get { return _itemsVisible; } 
      set 
      { 
       _itemsVisible = value; 
       NotifyPropertyChanged("ItemsVisible"); 
      } 
     } 

     //Return the Items Collection/Set the Items Collection and Notify. 
     public ObservableCollection<Item> ItemsCollection 
     { 
      get 
      { 
       return _itemsCollection; 
      } 
      set 
      { 
       _itemsCollection = value; 
       NotifyPropertyChanged("ItemsCollection"); 
      } 
     } 

     #endregion 

     #region Constructor 

     /// <summary> 
     /// Items Constructor 
     /// </summary> 
     public Items() 
     { 
      _itemsVisible = true; 
      _itemsCollection = new ObservableCollection<Item>(); 
     } 

     #endregion 

     #region Methods 

     /// <summary> 
     /// Add Item to the ItemsCollection. 
     /// </summary> 
     /// <param name="item">Item Object</param> 
     public void AddItem(Item item) 
     { 
      //Bind item.Visible to this.ItemsVisible 
      _itemsCollection.Add(item); 
     } 

     #endregion 
    } 
} 
+0

項目是項目的成員。所以當Items.ItemsVisible屬性值的值發生變化時。您可以遍歷集合中的每個項目並設置相同的值。 – Nps

+0

我知道我可以循環...我試圖找到「更好」的方式,然後循環,這就是爲什麼我想知道是否有可能綁定他們 – Ron

+0

你打算如何得到一個真/假或可見/不是一個集合? – Paparazzi

回答

2

ItemsItem屬性中設置數據綁定只是從正確的接口監聽PropertyChangedCollectionChanged事件。

您可以使用+=子句認購,或WeakEventListener模式,使用PropertyChangedEventManagerCollectionChangedEventManager

我喜歡最後一個,因爲:

監聽事件可能會導致內存泄漏。

所以,你Items類應該實現IWeakEventListener接口:

public class Items : INotifyPropertyChanged, IWeakEventListener 
{ 
    #region IWeakEventListener 

    public bool ReceiveWeakEvent(Type managerType, Object sender, EventArgs e) 
    { 
     if (sender == this._itemsCollection && managerType == typeof(CollectionChangedEventManager)) 
     { 
      // Your collection has changed, you should add/remove 
      // subscription for PropertyChanged event 
      UpdateSubscriptions((NotifyCollectionChangedEventArgs)e); 
      return true; 
     } 
     if (sender is Item && managerType == typeof(PropertyChangedEventManager)) 
     { 
      // The Visible property of an Item object has changed 
      // You should handle it properly here, for example, like this: 
      this.ItemsVisible = this._itemsCollection.All(i => i.Visible); 
      return true; 
     } 

     return false; 
    } 

    private void UpdateSubscriptions(NotifyCollectionChangedEventArgs e) 
    { 
     switch(e.Action) 
     { 
      case NotifyCollectionChangedAction.Add: 
       foreach (Item item in e.NewItems) 
       { 
        PropertyChangedEventManager.AddListener(item, this, "Visible"); 
       } 
       break; 
      case NotifyCollectionChangedAction.Remove: 
       foreach (Item item in e.OldItems) 
       { 
        PropertyChangedEventManager.RemoveListener(item, this, "Visible"); 
       } 
       break; 
      case NotifyCollectionChangedAction.Reset: 
       foreach (Item item in this._itemsCollection) 
       { 
        PropertyChangedEventManager.RemoveListener(item, this, "Visible"); 
        PropertyChangedEventManager.AddListener(item, this, "Visible"); 
       } 
       break; 
      default: 
       break; 
     } 
    } 

... 
    public Items() 
    { 
     _itemsVisible = true; 
     _itemsCollection = new ObservableCollection<Item>(); 
     CollectionChangedEventManager.AddListener(_itemsCollection, this); 
    } 
} 
+0

我理解你的方法,當我尋找更簡單的解決方案,然後遍歷我的項目時,你提供了更復雜的東西(使用偵聽器) - 我認爲它是更復雜? – Ron

+0

我只是告訴你更多的可能性來完成屬性更新,我解釋了爲什麼你應該嘗試這種方法。如果在某些Item.Visible發生更改時需要更改ItemsVisible,則這是必需的。如果你只需要改變Item.Visible屬性,你可以在ItemsVisible設置器中簡單地做到這一點。 – stukselbax

0

在WPF意義僅適用於DependencyProperties和兩個標準性能(即使使用INotifyPropertyChanged的時候)之間不適用綁定。也就是說,如果您將這些類用作View Models並將它們綁定到控件,那麼當ItemsVisible和Visible屬性都爲true時(例如),您可以使用MultiConverter將控件的可見性設置爲摺疊。

或者,您可以將一個Parent屬性添加到Item類並將其設置爲父項目類,它將允許您讓Item.Visible屬性返回父項的ItemsVisible屬性(或者在應用程序中再次使用任何邏輯)。

相關問題