C#WPF如何动态设置Property setter方法? [英] C# WPF How to set Property setter method dynamically?

查看:3397
本文介绍了C#WPF如何动态设置Property setter方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在搜索,但我似乎找不到我要找的东西,所以我会去这里。



情况:
我有类MainWindow和MainWindowData。在MainWindowData中只有使用属性UpdateGUI定义的公共属性。

  public class UpdateGUI:Attribute {} 

public class MainWindowData
{
[UpdateGUI]
public string TESTVAR {get;组; }
}

现在我要在MainWindowData中为每个属性的setter方法添加一个方法。更具体:

  void OnPropertyChanged(String PropertyName); 

我想我会在MainWindow构造函数中获取所有UpdateGUI属性,然后以某种方式添加另一种方法它,但这是我被卡住的地方。
我使用这段代码来获取所有的属性,它们有效:

 列表< PropertyInfo> property =(from type in typeof(MainWindowData).GetProperties()
其中pi.GetCustomAttributes(typeof(UpdateGUI),false).Any()
select pi).ToList();

这给我一个很好的列表,我必须更新的所有属性。



所以问题是:我如何使它能够动态地从:

  [UpdateGUI] 
public string TESTVAR {get;组;

to:

  [UpdateGUI] 
私有字符串_TESTVAR;
public string TESTVAR {
get {
return _TESTVAR;
}
set {
_TESTVAR = value;
OnPropertyChanged(TESTVAR);
}
}

感谢您的帮助!将非常感激:)



问候语

解决方案

正在寻找已经解决了面向方面编程(AOP)的概念。



一个例子是中的解决方案#uirel =nofollow>(另请参见查看详细信息< a>),它可以让你编写这样的数据/ viewmodel类:

  [NotifyPropertyChanged] 
public class Shape
{
public double X {get;组; }
public double Y {get;组; }
}

public class Rectangle:Shape
{
public double Width {get;组; }
public double Height {get;组; }
}

如果你不喜欢PostSharp,我确定其他AOP框架有类似的功能。



编辑



我刚刚发现 NotifyPropertyWeaver ,这样做不需要一个完整的AOP框架。



它使用Mono.Cecil的东西在编译时注入通知代码,可以通过NuGet(这是我做的)或从项目网站安装。 p>

默认情况下,它甚至不需要属性(它会自动显示哪些属性和类需要更改通知),但是您也可以这样显式:

  [NotifyProperty] 
public int FooBar {get;组; }

我发现一个很好的功能是声明属性之间的依赖关系的可能性。在这种情况下,当FooBar发生变化时,将调用 RaisePropertyChanged(FoobarTimesTwo)

  [DependsOn(FooBar)] 
public int FoobarTimesTwo
{
get {return FooBar * 2; }
}


I've been searching around but I just can't seem to find what I'm looking for, so I'll give it a go here.

Situation: I have the class MainWindow and MainWindowData. In MainWindowData are only public properties defined with the attribute UpdateGUI.

public class UpdateGUI : Attribute { }

public class MainWindowData
{
    [UpdateGUI]
    public string TESTVAR { get; set; }
}

Now I want to add a method to each property's setter method in MainWindowData. More specific:

void OnPropertyChanged(String PropertyName);

I figured I'd fetch all UpdateGUI properties in the MainWindow constructor, and then somehow add another method to it, but this is where I'm stuck. I use this code to fetch all properties, which works:

List<PropertyInfo> properties = (from pi in typeof(MainWindowData).GetProperties()
                                 where pi.GetCustomAttributes(typeof(UpdateGUI), false).Any()
                                 select pi).ToList();

This gives me a nice list of all properties that I have to update.

So the question is: how can I make it so that the properties dynamically get transformed from:

[UpdateGUI]
public string TESTVAR { get; set; }

to:

[UpdateGUI]
private string _TESTVAR;
public string TESTVAR { 
    get {
        return _TESTVAR;
    }
    set {
        _TESTVAR = value;
        OnPropertyChanged("TESTVAR");
    }
}

Thank you for any help! It will be highly appreciated :)

Greetings

解决方案

What you are looking for has been solved in the concept of Aspect Oriented Programming (AOP).

One example is in PostSharp, (Also, see details here) which lets you write your data/viewmodel classes like this:

[NotifyPropertyChanged]
public class Shape
{
    public double X { get; set; }
    public double Y { get; set; }
}

public class Rectangle : Shape
{
    public double Width { get; set; }
    public double Height { get; set; }
}

If you don't like PostSharp, I'm sure the other AOP frameworks out there has similar functionality.

EDIT

I just found NotifyPropertyWeaver which does this for you without requiring a full AOP framework.

It uses the Mono.Cecil stuff to inject notification code during compilation and is installable either through NuGet (this is what I did) or from the project web site.

By default, it doesn't even require attributes, (it automatically figures out which properties and classes need change notification) but you can be explicit also, like so:

[NotifyProperty]
public int FooBar { get; set; }

One nice feature I found in it was the possibility to declare dependencies between properties. In this case, RaisePropertyChanged("FoobarTimesTwo") will be called whenever FooBar changes.

[DependsOn("FooBar")]
public int FoobarTimesTwo
{
    get { return FooBar * 2; }
}

这篇关于C#WPF如何动态设置Property setter方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆