0

I have a WPF ListBox of Grids that I create as follows:

I define the listbox in XAML with a simple declaration as follows:

<ListBox Name="MyListbox" >
</ListBox>

In code, I dynamically create an arbitrary number of Grid items (System.Windows.Controls.Grid), and I add them to the ListBox. Something like:

foreach (MyDataType myItem in MyDataList)
{
   Grid newGrid = new Grid();
   // Code that sets the properties and values of the grid, based on myItem

   MyListbox.Items.Add(newGrid);
}

This works great, and everything looks the way that I want it to.

Now, I've decided that in my ListBox, I also want to store a reference to the actual myItem object, so that I can reference it later.

My idea was to create a new class like:

public class ListGridItemNode
{
    public MyDataType theItem;
    public Grid theGrid;
    public ListGridItemNode(MyDataType inItem, Grid inGrid)
    {
        theItem = inItem;
        theGrid = inGrid;
    }
}

And then change my code to:

foreach (MyDataType myItem in MyDataList)
{
   Grid newGrid = new Grid();
   // Code that sets the properties and values of the grid, based on myItem

   MyListbox.Items.Add(new ListGridItemNode(myItem,newGrid));
}

Of course, now my listbox instead of displaying the grids, just displays the text "MyApp.ListGridItemNode".

My question is: How do I tell the ListBox to go a level deeper and display the actual Grids inside of each ListGridItemNode object?

I suspect that this has something to do with bindings, but I can't find any examples that work the way that I'm doing it. Most of what I'm finding only shows binding to a string within an object, not an entire control.

3
  • The reason that you don't find documentation on this is that this isn't intended to be done. You create controls, which bind to data, using item templates to determine how the data is displayed. You shouldn't be dynamically adding to the items control like that. Commented Sep 10, 2014 at 21:32
  • Do not create UI elements in code. Instead, create a proper DataTemplate for your ListBox items. Take a look at the Data Templating Overview article on MSDN. Commented Sep 10, 2014 at 21:38
  • Actually this is strange, the other way around (ListBoxes in a Grid) is understandable. How are you going to deal with the blue focus over your grids ? If you need a collection of grids, wrap them inside an ItemsControl or a StackPanel for instance. Commented Sep 10, 2014 at 21:53

1 Answer 1

1

Couldn't you just use the Tag property of the Grid object?

newGrid.Tag = myItem;

Then later:

Grid grid; // obtain Grid object somehow
MyItem myItem = (MyItem) grid.Tag;
Sign up to request clarification or add additional context in comments.

1 Comment

That's a great alternate solution, and it worked for me. If nobody else has a solution to my original idea within a few days, I'll accept this one.

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.