Question

I have 4 ListViews.
The first ListView, is the main. It holds Custom UserControls - each UserControl has Image and Label.

You can add Items from the main ListView to any of the other 3 ListViews using the following code:

foreach (WindowItem wi in listView1.SelectedItems)
{
    listView2.Items.Add(wi.Clone());
}

The Custom UserControl has the following function:

public WindowItem Clone()
{
    return new WindowItem(window);
}

What it does, is returning new UserControl based on the original.

I want to link them somehow, so if I update the Image / Label of the orignial item, it will also update the similar Item in the other ListViews

Right now, what I am doing is when I need to update the other items, I use loop to check if the Items match the updated Item and if they do, I update them aswell - I hope / belive there's a better way...

Was it helpful?

Solution

You could create an event in the WindowItem that gets raised when the Image/Label are changed. From there you'd just have the cloned controls listen to the event and update on the call.

Something along the lines of: Create the event:

public delegate void DataUpdatedEvent(Image newimage, string newlabel);
public event DataUpdatedEvent DataUpdated;

Create a notification method, which you'd put a call to whenever Image or Label are changed:

private void NotifyDataChanged()
{ 
     if (DataUpdated != null) DataUpdated(this.Image, this.Label);
}

Plus a method for the cloned controls to call when the event is raised:

public void UpdateData(Image newimage, string newlabel)
{
    this.Image = newimage;
    this.Label = newlabel;
}

Then as you create the clones, have them listen in:

foreach (WindowItem wi in listView1.SelectedItems)
{
    WindowItem newWi = wi.Clone();
    wi.DataUpdated += new WindowItem.DataUpdatedEvent(newWi.UpdateData);
    listView2.Items.Add(newWi);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top