【问题标题】:Monitor a change in the property of a Telerik ScheduleView control in WPF监视 WPF 中 Telerik ScheduleView 控件的属性更改
【发布时间】:2012-03-19 02:30:41
【问题描述】:

我有一个类的 2 个属性(WPF 控件):HorizontalOffsetVerticalOffset(都是公共的Double)。每当这些属性发生变化时,我都想调用一个方法。我怎样才能做到这一点?我知道一种方法 - 但我很确定这不是正确的方法(使用非常短的滴答间隔的 DispatcherTimer 来监控属性)。

编辑更多内容:

这些属性属于 Telerik scheduleview 控件。

【问题讨论】:

  • 我知道如何订阅现有活动 - 但我没有创建自己的活动以供订阅的经验 - 这可能吗?这是您所说的最有效的方式吗?
  • 好吧,鉴于这是您不拥有的类型的两个属性;您需要查看 Telerik 在控件上公开了哪些机制(如果有)来监视这些属性。鉴于它是 WPF,我会认为它是 INotifyPropertyChanged。在这种情况下,您没有公开自己的事件源,您需要希望该控件上已经存在一个事件源
  • 那我现在就去看看。但是假设还没有一种方法可以监控这些属性的变化 - 没有其他方法可以观察属性吗??
  • 不是,不是。当然,使用计时器不是前进的方向。看一眼(必须说有点粘)Telerik API 文档,该控件似乎实现了 INotifyPropertyChanged;因此,您订阅它的 PropertyChanged 事件并在 args.PropertyName 为 "HorizontalOffset""VerticalOffset" 时执行某些操作

标签: c# properties propertychanged


【解决方案1】:

利用控件的INotifyPropertyChanged 接口实现。

如果控件调用myScheduleView

//subscribe to the event (usually added via the designer, in fairness)
myScheduleView.PropertyChanged += new PropertyChangedEventHandler(
  myScheduleView_PropertyChanged);

private void myScheduleView_PropertyChanged(Object sender,
  PropertyChangedEventArgs e)
{
  if(e.PropertyName == "HorizontalOffset" ||
     e.PropertyName == "VerticalOffset")
  {
    //TODO: something
  }
}

【讨论】:

    【解决方案2】:

    我知道一种方法...DispatcherTimer

    哇避免这种情况:) INotifyPropertyChange 接口是你的朋友。有关示例,请参阅the msdn

    您基本上在您的属性的Setter 上触发一个事件(通常称为onPropertyChanged),然后订阅者处理它。

    msdn 的示例实现如下:

    // This is a simple customer class that 
    // implements the IPropertyChange interface.
    public class DemoCustomer  : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;    
        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
              PropertyChanged(this, new PropertyChangedEventArgs(info));            
        }
    
        public string CustomerName
        {
            //getter
            set
            {
                if (value != this.customerNameValue)
                {
                    this.customerNameValue = value;
                    NotifyPropertyChanged("CustomerName");
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-20
      • 1970-01-01
      • 1970-01-01
      • 2013-01-09
      • 1970-01-01
      • 2012-05-28
      相关资源
      最近更新 更多