C#INotifyPropertyChanged的一个动态创建的对象的属性? [英] C# INotifyPropertyChanged on properties of a dynamically created object?

查看:969
本文介绍了C#INotifyPropertyChanged的一个动态创建的对象的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(更新)为我的Windows ICustomTypeDescriptor工作窗体应用程序,而不是Silverlight的;不支持。我会继续调查,虽然这个想法,看看我拿的。
(/更新)

(update) ICustomTypeDescriptor works for my Windows Forms app, but not for Silverlight; Not supported. I will keep investigating this idea though and see where i get to. (/update)

我有,说几句开关面板(对于那些喜欢类比)。
每一个开关面板有一个名字(字符串)开关可以在开启或关闭状态(布尔)。
中的switchpanel和开关是具有对它们的inotify接口对象

I have, say a few switch panels (for those that like analogies). Each of these switch panels has switches that have a Name(string) can be in state(bool) of On or Off. The switchpanel and switches are objects that have INotify interface on them.

使用交换机名称,我在集合创建的所有可能的开关的名称列表,并创建具有所有这些名称作为属性动态类。

Using the switches Names, I create a list of all possible switch names over the collection and create a dynamic class that has all these Names as properties.

SwitchPanel1 (Switches( Switch1 ("Main",On) , Switch2("Slave",Off)))
SwitchPanel2 (Switches( Switch1 ("Bilge",On) , Switch2("Main",Off)))

生成的

(Main,Bilge,Slave)

和动态类生产具有属性:

And a dynamic class is produced that has the properties:

SwitchPanel : (SwitchPanel)
Main : (Switch)
Bilge : (Switch)
Slave: (Switch)

我们的想法是,如果开关面板具有与属性的名称的开关,它被放置在该属性。因此,使用了一点的LINQ

The idea is that if the switch panel has a switch with the Name of the property, it is placed on that property. So using a bit of linq

propeties["Main"].SetValue(newSwitchType,SwitchPanel.Switches.FirstOrDefault(sw => sw.Name == "Main"));



我想这个新的动态类转换为INotfyPropertyChanged和捕捉这些新特性的实际变化,所以如果开关的状态改变动态对象将报告。

I want to cast this new dynamic class to INotfyPropertyChanged AND catch the actual changes on these new properties, so if a switch changes state the dynamic object will report it.

为什么呢?它需要显示在列表视图中,我使用有其通过提供属性名称的结合,而不是绑定路径列表视图。

Why? It needs to be displayed in a list view and the list view I'm using has its binding by supplying the Property name, and not the binding path.

它还试图通过对铸件的INotifyPropertyChanged的对象捕捉inotify事件。这意味着,当情况发生变化,将排序和/或组。

It also attempts to catch INotify events by casting the object against INotifyPropertyChanged. This means it will sort and/or group when things change.

如果你知道一个更好的办法来做到这一点让我知道。请。

If you know of a better way to do this let me know. Please.

推荐答案

您也许并不需要动态类。您可以通过 ICustomTypeDescriptor / 的GetProperties实现运行时绑定的属性(),创建自己的的PropertyDescriptor 实现,返回指定的开关。目前还不清楚第一次关于变更知道是什么,但你既可以使用 INotifyPropertyChanged的或更早的特定属性更改事件,再次绑每个属性(因此每个的PropertyDescriptor 重视,例如,在命名开关事件。

You probably don't need a dynamic class. You can implement runtime binding properties via ICustomTypeDescriptor / GetProperties(), creating your own PropertyDescriptor implementation that returns the named switch. It isn't clear what knows first about the change, but you could either use INotifyPropertyChanged, or the older property-specific change event, again tied to each property (so each PropertyDescriptor attaches to, for example, the event in the named switch.

不是小事,但不是不可能的要么。

Not trivial, but not impossible either.

这篇关于C#INotifyPropertyChanged的一个动态创建的对象的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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