问题
Where in the visual tree can I find the ComboBox popup (the list with the ComboBoxItems)?
I have programmatically opened a ComboBox and when watching it in the WPF Tree Visualizer in the debugger I see the following:
: ComboBox
templateRoot : Grid
PART_Popup : Popup
toggleButton : ToggleButton
templateRoot : Border
splitBorder : Border
Arrow : Path
contentPresenter : ContentPresenter
: TextBlock
I expected to see a ScrollViewer with some kind of item host (StackPanel?), perhaps where the PART_Popup is, but nothing.
So where is it?
回答1:
PART_Popup does have StackPanel with ItemsHost set to True and wrapped around by ScrollViewer. You can check out the default template here at MSDN.
This is how it looks like:
<Popup x:Name="Popup"
Placement="Bottom"
IsOpen="{TemplateBinding IsDropDownOpen}"
AllowsTransparency="True"
Focusable="False"
PopupAnimation="Slide">
<Grid x:Name="DropDown"
SnapsToDevicePixels="True"
MinWidth="{TemplateBinding ActualWidth}"
MaxHeight="{TemplateBinding MaxDropDownHeight}">
<Border x:Name="DropDownBorder"
BorderThickness="1">
<Border.BorderBrush>
<SolidColorBrush Color="{DynamicResource BorderMediumColor}" />
</Border.BorderBrush>
<Border.Background>
<SolidColorBrush Color="{DynamicResource ControlLightColor}" />
</Border.Background>
</Border>
<ScrollViewer Margin="4,6,4,6"
SnapsToDevicePixels="True">
<StackPanel IsItemsHost="True"
KeyboardNavigation.DirectionalNavigation="Contained" />
</ScrollViewer>
</Grid>
</Popup>
UPDATE
PopUp and comboBox doesn't share the same root. They belong to different Visual Tree, that's why not visible in WPF Tree Visualizer since PopUp needs to be opened to see it's Visual Tree.
You can use Snoop which is WPF spying utility which also has feature to inspect Visual Tree. Snapshot taken from Snoop for popup looks like this (Windows 8):
来源:https://stackoverflow.com/questions/24088824/where-is-combobox-popup-in-visual-tree