1

I want to get the actual height of the ComboBox plus the height of its DropDown when it is opened. However there does not appear to be an accessible property on the ComboBox to give this.

The ComboBox.ActualHeight property only gives the height of the base ComboBox, not its dropdown.

This one is kind of infuriating as I think I can see the value set on a property in the debugger, but the property is not accessible in code for some reason - ItemsHost.ActualHeight.

See below:

ActualHeight is accessible

However, ItemsHost which appears to have the height of the dropdown is not accessible from code!

enter image description here

  • Not sure why it is not accessible but try getting height of one item * number of items + height of combobox – Aleksa Ristic Oct 03 '17 at 15:19
  • As it happens that is what I am doing for now! It works but is not pixel perfect. The ComboBox is slightly higher than the Items in the drop down. – Andrew Scott Oct 03 '17 at 15:35
  • As it happens I was able to deduct the Padding top and bottom of the ActualHeight to get an accurate estimation of the height of each item in the drop down. – Andrew Scott Oct 03 '17 at 15:44

1 Answers1

1

The property is not accessible because it is not defined as public. You could get its value using reflection:

Get property value from string using reflection in C#

You shouldn't really do this though as the property is non-public for a reason.

Instead, you could get a reference to the Popup element of the ComboBox and check the ActualHeight property of its Child, e.g.:

ComboBox cmb = sender as ComboBox;
double heightOfComboBox = cmb.ActualHeight;

double heightOfPopup = 0.0;
Popup popup = cmb.Template.FindName("PART_Popup", cmb) as Popup;
if (popup != null)
{
    FrameworkElement fe = popup.Child as FrameworkElement;
    if (fe != null)
        heightOfPopup = fe.ActualHeight;
}

double totalHeight = heightOfComboBox + heightOfPopup;
mm8
  • 163,881
  • 10
  • 57
  • 88
  • Don't worry I did upvote! But I don't have enough reputation for it to register sadly. EDIT - Looks like I do since yesterday, have my upvote!! – Andrew Scott Oct 04 '17 at 12:23