views:

249

answers:

2

How can I create a binding to a sub-property of a specific element in a list?

I've created a class that exposes an IList property:

    public IList<VideoChannel> VideoChannels {
        get {
            const int NumVideoChannels = 4;

            return (new List<VideoChannel>(NumVideoChannels) {

                new VideoChannel("Channel 1") { 
                    VideoActive = !_rawData[Main][0x04].BitIsSet(0), 
                    OutOfRange = !_rawData[Main][0x05].BitIsSet(0) },

                new VideoChannel("Channel 2") { 
                    VideoActive = !_rawData[Main][0x04].BitIsSet(1), 
                    OutOfRange = !_rawData[Main][0x05].BitIsSet(1) },

                new VideoChannel("Channel 3") { 
                    VideoActive = !_rawData[Main][0x04].BitIsSet(2), 
                    OutOfRange = !_rawData[Main][0x05].BitIsSet(2) },

                new VideoChannel("Channel 4") { 
                    VideoActive = !_rawData[Main][0x04].BitIsSet(3), 
                    OutOfRange = !_rawData[Main][0x05].BitIsSet(3) },

            }).AsReadOnly();
        }
        set { ;}
    }

I've also created an 'LED' UserControl with a single boolean property ('LedOn') that determines the colour of the led.

I want to create 8 'LED' controls, each of which is bound to a specific 'VideoActive' or 'OutOfRange' property in the IList above.

This doesn't seem to work:

        ledVideoActiveChannel1.DataBindings.Add("LedOn", _myDevice, "VideoChannels[0].VideoActive");

        ledOutOfRangeChannel1.DataBindings.Add("LedOn", _myDevice, "VideoChannels[0].OutOfRange");

The error is "Child list for field VideoChannels[0] cannot be created."

I'm relatively new to C# and OOP in general, so forgive me if this is a trivial question.

Thanks!

+1  A: 

How about this:

ledVideoActiveChannel1.DataBindings.Add("LedOn", _myDevice.VideoChannels[0], "VideoActive");
ledOutOfRangeChannel1.DataBindings.Add("LedOn", _myDevice.VideoChannels[0], "OutOfRange");

That, and make sure your VideoChannel class implements INotifyPropertyChanged.

BFree
I've already implemented INotifyPropertyChanged on VideoChannel. Your suggestion using '_myDevice.VideoChannels[0]' seems to compile just fine, however updates don't seem to propagate. Could it be because _myDevice is returning a NEW List<VideoChannels> on each 'get' request?
My mistake, your suggestion works perfectly. Thanks!
+1  A: 

that would be a magical "magic string" ;-)

You can create separate property like so:

public bool OutOfRange
{
  get{ return VideoChannels[0].OutOfRange; }
}

Then

ledOutOfRangeChannel1.DataBindings.Add("LedOn", _myDevice, "OutOfRange");

You would want to add null checking also...

mxmissile
Yes, this was my first approach and worked just fine. '_myDevice' is made up of a number of other similar properties as well (SerialDataChannels, VideoChannels, BackplaneChannels, etc.). I felt like exposing a couple of ILists was a cleaner approach - one List for each collection of channels vs. 50-odd individual properties.