2014-03-02 32 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属性(或者在应用程序中再次使用任何逻辑)。

相关问题