Anee
Anee

Reputation: 483

ComboBox template issue

I came across a weird problem when i was trying to template a WPF ComboBox. I have a 3rd party dll which serializes the given control into a string. When i give the ComboBox which has the following template, i get an error saying "Exception has been thrown by the target of an invocation.A panel with IsItemsHost="true" is not nested in an ItemsControl. Panel must be nested in ItemsControl to get and show items."

<Style x:Key="ComboBoxStyle" TargetType="{x:Type ComboBox}">
    <Setter Property="SnapsToDevicePixels" Value="true"/>
    <Setter Property="OverridesDefaultStyle" Value="true"/>
    <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
    <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
    <Setter Property="ScrollViewer.CanContentScroll" Value="true"/>
    <Setter Property="Width" Value="{Binding}"/>
    <Setter Property="Height" Value="{Binding}"/>
    <Setter Property="Foreground" Value="White"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ComboBox}">
                <Grid>
                    <ToggleButton 
                        Name="ToggleButton" 
                        Template="{StaticResource ComboBoxToggleButton}" 
                        Grid.Column="2" 
                        Focusable="false"
                        Foreground="{TemplateBinding Foreground}"
                        Background="{TemplateBinding Background}"
                        IsChecked="{Binding Path=IsDropDownOpen,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}"
                        ClickMode="Press">
                    </ToggleButton>
                    <ContentPresenter Name="ContentSite" IsHitTestVisible="False"  Content="{TemplateBinding SelectionBoxItem}"
                        ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"
                        ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"
                        Margin="3,0,23,3"
                        VerticalAlignment="Center"
                        HorizontalAlignment="Center" />
                    <TextBox x:Name="PART_EditableTextBox"
                        Style="{x:Null}" 
                        Template="{StaticResource ComboBoxTextBox}" 
                        HorizontalAlignment="Left" 
                        VerticalAlignment="Center" 
                        Margin="3,0,23,3"
                        Focusable="True" 
                        Background="{TemplateBinding Background}"
                        Foreground="{TemplateBinding Foreground}"
                        Visibility="Hidden"
                        IsReadOnly="{TemplateBinding IsReadOnly}"/>
                    <Popup 
                        Name="Popup"
                        Placement="Bottom"
                        IsOpen="{TemplateBinding IsDropDownOpen}"
                        AllowsTransparency="True" 
                        Focusable="False"
                        PopupAnimation="Slide">

                        <Grid Name="DropDown"
                          SnapsToDevicePixels="True"                
                          MinWidth="{TemplateBinding ActualWidth}"
                          MaxHeight="{TemplateBinding MaxDropDownHeight}">
                            <Border 
                            x:Name="DropDownBorder"
                            Background="{TemplateBinding Background}"
                            BorderThickness="1"
                            BorderBrush="#888888"/>
                            <ScrollViewer Margin="4,6,4,6" SnapsToDevicePixels="True">
                                **<StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained" />**
                            </ScrollViewer>
                        </Grid>
                    </Popup>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
    </Style.Triggers>
</Style>

The style works properly on the control but the serializing gives me problems. I suspect that '**' in the code snippet i pasted has a problem. Is there a different way of achieving combobox styling?

Upvotes: 2

Views: 886

Answers (1)

Joost van den Boom
Joost van den Boom

Reputation: 184

ComboBox is an ItemsControl. This means that an ItemsPresenter is expected in the template (at the place where your **StackPanel is). If you want to change the panel the ComboBox uses to render the items, change the ItemsPanel template of the ComboBox.

Upvotes: 2

Related Questions