1、写一个监听值变化的类
public class MonitorValueChange { private Visibility myValue; public Visibility MyValue { get { return myValue; } set { if (value != myValue) { WhenMyValueChange(); } myValue = value; } } //定义的委托 public delegate void MyValueChanged(object sender, EventArgs e); //与委托相关联的事件 public event MyValueChanged OnMyValueChanged; //事件触发函数 private void WhenMyValueChange() { if (OnMyValueChanged != null) { OnMyValueChanged(this, null); } } }
2、调用该类监听数据
MonitorValueChange change = new MonitorValueChange(); change.MyValue = Visibility.Visible; change.OnMyValueChanged += Change_OnMyValueChanged; change.MyValue = Visibility.Collapsed; private void Change_OnMyValueChanged(object sender, EventArgs e) { //要做的操作 }