ComboBox模板问题

时间:2014-11-24 09:02:35

标签: wpf templates combobox styles

当我尝试模板化WPF ComboBox时,我遇到了一个奇怪的问题。我有一个第三方dll,它将给定控件序列化为一个字符串。当我给出具有以下模板的ComboBox时,我得到一个错误,说“调用的目标已抛出异常。具有IsItemsHost =”true的面板未嵌套在ItemsControl中。面板必须嵌套在ItemsControl中得到并展示物品。“

<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>

该样式在控件上正常工作,但序列化给我带来了问题。我怀疑我粘贴的代码段中的'**'有问题。是否有不同的方式来实现组合框样式?

1 个答案:

答案 0 :(得分:1)

ComboBoxItemsControl。这意味着模板中需要ItemsPresenter(在**StackPanel所在的位置)。如果您要更改ComboBox用于呈现项目的面板,请更改ItemsPanel的{​​{1}}模板。

相关问题