繁体   English   中英

WPF ::自定义控件如何在用户更改属性时触发方法

[英]WPF :: Custom control how to Fire a method when a user change a property

您好,我创建了一个自定义控件类,该类从Grid派生,所以当用户更改属性时,是否存在要覆盖的函数? 例如,如果我有一个名为Count的属性,我如何知道用户何时更改count属性?

非常感谢

编辑

我正在进步

受保护的重写void OnPropertyChanged(System.Windows.DependencyPropertyChangedEventArgs e){

}

现在在我的设计器窗口上,当我更改属性时,自定义控件不会更改,我将必须运行代码并关闭它以查看我的设计器窗口上的差异,我该如何解决呢?

如果它是依赖项属性,则使用:

public static readonly DependencyProperty CountProperty =
                DependencyProperty.Register("Count", typeof(int), typeof(YourClass), new UIPropertyMetadata(OnCountChanged));

public int Value
{
       get { return (int)GetValue(CountProperty); }
       set { SetValue(CountProperty, value); }
}

private static void OnCountChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
{
       YourClass cp = obj as YourClass;
       MethodToExecute();
}

您可以订阅NotifyPropertyChanged事件:

在构造函数中输入以下内容:

this.NotifyPropertyChanged += (sender, eventargs) =>
{
   if (eventargs.PropertyName == "Count")
      this.CountChanged();
};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM