I’m trying to create a grid, and set it as the content for a ListBoxItem. I’m doing this in C# in PrepareContainerForItemOverride() method of ListBox class for performance reasons.
protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
{
base.PrepareContainerForItemOverride(element, object);
Grid grid = new Grid();
... adding stuff to the grid ...
ListBoxItem lbItem = element as ListBoxItem;
lbItem.Content = grid;
}
I can set some string to the content, but not the grid.
This had to be done, even though there was no ContentTemplate definition in xaml. ListBox had some default content template that was messing this.