getting the size of the combobox's popup at runtime
I am trying to get the width and height of a Silverlight ComboBox's dropdown window.
Unfortunately ActualWidth
开发者_JS百科and ActualHeight
are returning 0 always.
<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.
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.)
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.
精彩评论