views:

1161

answers:

1

This is the same Question as one I asked earlier BUT that one was in reference to normal C#. This is Silverlight 2, and I don't have 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.

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

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.

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

A: 

You could create a derived generic Dictionary of string, bool that implements INotifyPropertyChanged. 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:

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.

James Cadd
Interesting. Will give it a go soon.
burnt_hand