简体   繁体   English

Silverlight 2:在动态创建的对象上INotifyPropertyChanged?

[英]Silverlight 2: INotifyPropertyChanged on dynamically created object?

This is the same Question as one I asked earlier BUT that one was in reference to normal C#. 这与我之前问过的一个问题是相同的,但我曾问过它是关于普通C#的。 This is Silverlight 2, and I don't have ICustomTypeDescriptor 这是Silverlight 2,我没有ICustomTypeDescriptor

So here is the question again: 所以这又是一个问题:

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. 配电盘和开关是在其上具有INotify接口的对象。

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)))

Produces a collection of 产生一个集合

(Main,Bilge,Slave)

And a dynamic class is produced that has the properties: 并生成具有以下属性的动态类:

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

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 所以用一点linq

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

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. 我想将此新动态类强制转换为INotfyPropertyChanged并捕获这些新属性的实际更改,因此,如果开关更改状态,则动态对象将报告它。

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. 它需要显示在列表视图中,而我正在使用的列表视图通过提供属性名称而不是绑定路径来进行绑定。

It also attempts to catch INotify events by casting the object against INotifyPropertyChanged. 它还尝试通过将对象强制转换为INotifyPropertyChanged来捕获INotify事件。 This means it will sort and/or group when things change. 这意味着它将在事物发生变化时进行排序和/或分组。

You could create a derived generic Dictionary of string, bool that implements INotifyPropertyChanged. 您可以创建实现INotifyPropertyChanged的字符串bool的派生泛型字典。 The indexer can look like this: 索引器如下所示:

public new bool this[string key]
{
    get
    {
        if( this.ContainsKey(key))
           return base[key];
        return default(bool);
    }
    set
    {
        base[key] = value;
        OnPropertyChanged(key.ToString());
    }
}

The in your switch panel use a custom IValueConverter to bind the switches to the dictionary: 开关面板中的使用自定义IValueConverter将开关绑定到字典:

http://silverlight.net/forums/t/51864.aspx http://silverlight.net/forums/t/51864.aspx

That way you can still have a dynamic collection of Names, each with an associated bool value, and bind directly to the data without creating a dynamic type. 这样,您仍然可以拥有一个动态的Names集合,每个Names都有一个关联的bool值,并直接绑定到数据而无需创建动态类型。

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

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