简体   繁体   English

在设计模式C#WPF中检查属性更改的正确方法?

[英]Correct way to check for property change in design mode C# WPF?

Ok, so I'm trying to run some code that modifies a UI when a user changes a custom control's dependency property value in design mode; 好的,所以我试图运行一些代码,以在用户在设计模式下更改自定义控件的依赖项属性值时修改UI。 but only when in design mode. 但仅当处于设计模式时。

I've tried these approaches: 我尝试了以下方法:

1.
public static DependencyProperty x = ...Register(..., new PropertyMetadata(null, changeMethod));

2.
set { SetValue(XProp, value); changeMethod(value); }

3.
var observable = x as INotifyPropertyChanged;
observable.PropertyChanged += ObservablePropertyChanged;

But all of them seem to have their own issues in that they either trigger errors or don't work at all. 但是它们似乎都存在自己的问题,因为它们要么触发错误,要么根本不起作用。

So does anyone know what the correct way to listen to a dependency property change in design mode is, and if so can you give an example? 那么,有谁知道在设计模式下侦听依赖项属性更改的正确方法是什么,如果可以,您可以举个例子吗?

The right way to handle DependencyProperty changes is to: 处理DependencyProperty更改的正确方法是:

. Declare the DependencyProperty: 声明DependencyProperty:

public static DependencyProperty MyXProperty;

. Create the public get/set Property: 创建公共获取/设置属性:

public string MyX
{
    get { return (string)GetValue(MyXProperty); } //Supposing that the property type is string
    set { SetValue(MyXProperty, value); }
}

. Register the DependencyProperty in your static constructor: 在您的静态构造函数中注册DependencyProperty:

static MyClass()
{
    MyXProperty= DependencyProperty.Register("MyX", typeof(string), typeof(MyClass), new FrameworkPropertyMetadata("", OnMyXPropertyChanged));
}

. Declare the Property Changed Method: 声明属性更改方法:

private static void OnMyXPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
{
    MyClass thisClass = d as MyClass ;
    //Do Something            
}

Please provide more information if you still can't find your solution. 如果仍然找不到解决方案,请提供更多信息。

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

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