C# 将一个类中的值绑定到另一个类中的另一个值
我有以下课程。C# 将一个类中的值绑定到另一个类中的另一个值,c#,wpf,binding,C#,Wpf,Binding,我有以下课程。 我想绑定Items.Visible到Items.ItemsVisible-可能吗?如果可能-如何 项目1.cs: using System; using System.ComponentModel; namespace WpfApplication85 { /// <summary> /// Item Object. /// </summary> public class Item : INotifyPropertyCha
我想绑定Items.Visible到Items.ItemsVisible-可能吗?如果可能-如何 项目1.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
}
}
使用系统;
使用系统组件模型;
命名空间WpfApplication85
{
///
///项目对象。
///
公共类项目:INotifyPropertyChanged
{
#区域inotifyproperty已更改
公共事件PropertyChangedEventHandler PropertyChanged;//属性更改时要通知的事件。
///
///通知属性已更改。
///
///属性的名称
受保护的void NotifyPropertyChanged(字符串propertyName)
{
if(PropertyChanged!=null)
{
PropertyChanged(这是新的PropertyChangedEventArgs(propertyName));
}
}
#端区
#区域私有变量
private bool _Visible;//bool确定项目是否可见
#端区
#区域公共财产
//返回Visible的值/设置Visible和Notify的值。
公众视野
{
获取{return\u Visible;}
设置
{
_可见=价值;
NotifyPropertyChanged(“可见”);
}
}
#端区
#区域构造函数
///
///项目构造函数
///
公共项目()
{
_可见=真实;
}
#端区
}
}
项目1.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
}
}
使用系统;
使用System.Collections.ObjectModel;
使用系统组件模型;
命名空间WpfApplication85
{
///
///项目对象。
///
公共类项目:INotifyPropertyChanged
{
#区域inotifyproperty已更改
公共事件PropertyChangedEventHandler PropertyChanged;//属性更改时要通知的事件。
///
///通知属性已更改。
///
///属性的名称
受保护的void NotifyPropertyChanged(字符串propertyName)
{
if(PropertyChanged!=null)
{
PropertyChanged(这是新的PropertyChangedEventArgs(propertyName));
}
}
#端区
#区域私有变量
private bool _itemsVisible;//bool确定项目是否可见
私有ObservableCollection _itemsCollection;//项的集合。
#端区
#区域公共财产
//返回ItemsVisible的值/设置ItemsVisible和Notify的值。
公共物品可见
{
获取{return\u itemsVisible;}
设置
{
_itemsVisible=值;
NotifyPropertyChanged(“ItemsVisible”);
}
}
//返回项目集合/设置项目集合并通知。
公共可观察收集项目收集
{
得到
{
返回_itemsCollection;
}
设置
{
_itemsCollection=值;
NotifyPropertyChanged(“ItemsCollection”);
}
}
#端区
#区域构造函数
///
///项目构造函数
///
公共物品()
{
_itemsVisible=true;
_itemsCollection=新的ObservableCollection();
}
#端区
#区域方法
///
///将项添加到ItemsCollection。
///
///项目对象
公共无效附加项(项目)
{
//绑定项。对此可见。项可见
_itemsCollection.Add(项目);
}
#端区
}
}
WPF意义上的绑定仅适用于DependencyProperties,不适用于两个标准属性(即使使用INotifyPropertyChanged)
也就是说,如果将这些类用作视图模型并将它们绑定到控件,则可以使用多转换器将控件的可见性设置为在ItemsVisible和Visible属性都为true时折叠(例如)
或者,您可以将父属性添加到Item类,并将其设置为允许您拥有Item的父Items类。Visible属性返回父属性的ItemsVisible属性(或者应用程序中任何有意义的逻辑).在
项
和项
属性中设置数据绑定只不过是从适当的接口侦听或事件
您可以为订阅使用+=
子句,也可以使用and模式
我更喜欢最后一个,因为:
侦听事件可能导致内存泄漏
因此,您的项
类应该实现接口:
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);
}
}
公共类项:INotifyPropertyChanged,IWeakEventListener
{
#区域IWeakEventListener
public bool ReceiveWeakEvent(类型managerType、对象发送者、事件参数e)
{
如果(发件人==此._项集合和管理类型==类型(集合更改开发管理器))
{
//您的收藏已更改,您应该添加/删除
//PropertyChanged事件的订阅
更新订阅((NotifyCollectionChangedEventArgs)e);
返回true;
}
if(发送方为Item&&managerType==typeof(PropertyChangedEventManager))
{
//项对象的可见属性已更改
//您应该在这里正确处理它,例如:
this.ItemsVisible=this.\u itemsCollection.All(i=>i.Visible);
返回true;
}
返回false;
}
私有void更新订阅(NotifyCollectionChangedEventArgs e)
{
开关(电动)
{
案例NotifyCollectionChangedAction。添加:
foreach(e.NewI中的项目