0

I've created a very basic custom control based on a listbox with two dependency properties. These dependency properties are called SourceA and SourceB. These two dependency properties are exposed for the user to bind to.The goal was for me to have multiple bind slots for collections. Which works great so far.

In my custom control I combine these two lists into one, which then gets displayed in the listbox as seen in the image below.

My question?

I need a way to distinguish the items from one another after combining them. So as I expand the custom control, I still know on the backend which items in the list came from either SourceA or SourceB. The reason being so i can add specific functionality to the items in SourceA vs SourceB.

Is there a way for me to tag the two incoming list items, within the custom control? Would an enum, interface, or wrapper be a solution?

DropBox Solution: https://www.dropbox.com/s/74su534n1szk91b/NexusEditor_03.zip?dl=0

<Grid>
    <nexus:NexusEditor 
        SourceB="{Binding ItemList}"
        SourceA="{Binding GroupList}"/>
</Grid>

Thanks

2
  • It is tough to assume lot of things, I suggest post some code and ask specific question. Commented Feb 5, 2016 at 2:51
  • @HariPrasad I've updated the question with the files. Let me know what you think. Commented Feb 5, 2016 at 12:01

1 Answer 1

1

Well, based on you current implementation, you would have to have a way to identify the type of object regardless of the source. So the easiest thing to do would be to create a wrapper object that you control and can add whatever information you want to track the objects. Something like this:

public class NexusItem
{
    public object Item { get; set; }
    public Brush Background { get; set; }
}

Then modify your UpdateItems method to add the tracking data:

        private static void UpdateItems(NexusEditor editor)
    {
        editor.Items.Clear();

        var sourceB = editor.SourceB as IEnumerable;
        if (sourceB != null)
        {
            foreach (object obj in sourceB)
            {
                var item = new NexusItem() { Item = obj, Background = new SolidColorBrush(Colors.Green) };
                editor.Items.Add(item);
            }
        }

        var sourceA = editor.SourceA as IEnumerable;
        if (sourceA != null)
        {
            foreach (object obj in sourceA)
            {
                var item = new NexusItem() { Item = obj, Background = new SolidColorBrush(Colors.Red) };
                editor.Items.Add(item);
            }
        }
    }

Now you can update your template to bind to the data you expect:

    <DataTemplate>
    <TextBlock Background="{Binding Background}" Grid.Column="1" Grid.Row="1" 
                Text="{Binding Item.Name}" 
                HorizontalAlignment="Center" VerticalAlignment="Center" />
    </DataTemplate>

enter image description here

Now, having said that, I think you will run into other issues and limitations based on your current control architecture as you are already making assumptions about the incoming objects in your templates. Maybe you won't and this will fit your needs just fine though.

Sign up to request clarification or add additional context in comments.

1 Comment

My templates are just temporary, im going to eventually make them so the user can define them. That way they can control what gets bound and what doesn't.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.