getting the size of the combobox's popup at runtime

816 Views Asked by At

I am trying to get the width and height of a Silverlight ComboBox's dropdown window. Unfortunately ActualWidth and ActualHeight are returning 0 always.

3

There are 3 best solutions below

0
On BEST ANSWER

I found a solution to this by myself: you have to set the popup's "IsOpen" to true before measuring, and then set it back to false. It's the only way I could make it work.

0
On

You can't get the actual size without actually rendering the popup. This implies that ActualSizes will be 0 if the popup is hidden. This is a consequence of WPF performing layout and rendering logic for you.

You could possibly get the popup's requested height by performing a Measure pass on the popup itself. If the popup hasn't been created yet, you're still in trouble though. (And it may not get created until the first time it's displayed.)

1
On
<ComboBox x:Name="comboBox" Height="20" Width="120">
    <ComboBox.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel SizeChanged="StackPanel_SizeChanged"/>
        </ItemsPanelTemplate>
    </ComboBox.ItemsPanel>
</ComboBox>

private void StackPanel_SizeChanged(object sender, SizeChangedEventArgs e)
{
   var w=   e.NewSize.Width;
   var h=  e.NewSizeHeight;
}

But it's not a good way.