本文介绍了将一个类别的值绑定到另一个类别的另一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下类要点与这些类.
我想将Item.Visible绑定到Items.ItemsVisible-是否可以?如果可以,如何?

I have the following classes gist with the classes.
I want to bind Item.Visible to Items.ItemsVisible - is it possible?, if so - how?

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
    }
}

推荐答案

ItemsItem属性中设置数据绑定只不过是监听 PropertyChanged CollectionChanged 事件来自适当的接口.

Setting data binding within Items and Item properties is nothing but listening PropertyChanged or CollectionChanged event from proper interfaces.

您可以使用+=子句进行订阅,也可以使用 WeakEventListener 模式,使用 PropertyChangedEventManager CollectionChangedEventManager

You can use either += clause for the subscription, or WeakEventListener pattern, using the PropertyChangedEventManager and CollectionChangedEventManager

我喜欢最后一个,因为:

I prefer the last one, because:

因此,您的Items类应实现 IWeakEventListener 接口:

So, your Items class should implement IWeakEventListener interface:

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

这篇关于将一个类别的值绑定到另一个类别的另一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-27 11:46
查看更多