This screenshot is from the mockup of my ideal UI. Right now, this is a DataGridTemplateColumn, with header = "ATTENDEES". I am running into issues creating the layout of this DataGridColumn's cell.

I currently have an ItemsControl bound to a List of strings which are the attendees' emails. If there are too many attendees and the ItemsControls' bounds cannot fit in the cell, then a Button with Content = "See more" should appear at the bottom of the cell, under the last attendee email that can be rendered within in the cell's bounds.

Then once the Button ("See more") is clicked, the row should expand to an appropriate height for the attendees to all be visible, and the "See more" Button should disappear.

I could not wrap my head around a clean implementation with a TemplateSelector, ValueConverter, or DataTrigger in pure XAML since I need to compare the ItemsControls' height against the DataGridRow's height and then perform a modification of the cell's layout at runtime by hiding all the items in the ItemsControl that cannot fit within the cell and then showing at Button below it.

I concluded on attempting to do this in the code-behind by subscribing to the ItemControls' load event. I first attempted to use the Height, MaxHeight, DesiredSize.Height, RenderedSize.Height, and ActualSize.Height properties of the ItemsControl but those all were equal to the clipped height of the ItemsControl, not the intrinsic height of all its contents.

I am now measuring the total height of all its items' strings using the FormattedText class. Then I compare this summed height with the row's height and that's as far as I have progressed; I am unsure of how to next change the layout of the cell or if this is even the correct approach.

I feel like I am fighting against the design of the WPF framework by doing rudimentary calculations and crude layout changes to the view in the code-behind.

Any help on this would be greatly appreciated!

Here is my event handler for the ItemsControl.Load:

private void AttendeesItemsControl_Loaded(object sender, RoutedEventArgs e) { if (currentRowIndex == -1) { return; } List<ModelBase> eventsData = ModelManager.events.data; var eventObj = (Event)eventsData[currentRowIndex]; var attendees = eventObj.attendees; var totalItemsHeight = 0; for(int i = 0; i < attendees.Count; i++) { totalItemsHeight += heightOfString(attendees[i]); } var itemsControl = (ItemsControl)sender; var controlRenderHeight = itemsControl.RenderSize.Height; // Check if the intrinsic height is greater than what can be drawn inside the cell if (controlRenderHeight < totalItemsHeight) { var itemHeight = totalItemsHeight / attendees.Count; var visibleItemsCount = controlRenderHeight / itemHeight; // .... not sure how to proceed } }

And the helper function that measures the height of one of its items:

private int heightOfString(string candidate) { var fontFamily = new FontFamily("Lato"); var fontStyle = FontStyles.Normal; var fontWeight = FontWeights.Normal; var fontStretch = FontStretches.Normal; var fontSize = 12; var typeFace = new Typeface(fontFamily, fontStyle, fontWeight, fontStretch); var formattedText = new FormattedText(candidate, CultureInfo.CurrentUICulture, FlowDirection.LeftToRight, typeFace, fontSize, Brushes.Black); return (int)formattedText.Height; }

Finally, this is the DataGridTemplateColumn's XAML, with the cell template definition: