组合框模板问题



当我尝试模板化WPF ComboBox时,我遇到了一个奇怪的问题。我有一个第三方 dll,它将给定的控件序列化为字符串。当我给出具有以下模板的组合框时,我收到一个错误,指出"调用目标已引发异常。具有 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>

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

ComboBox是一个ItemsControl。这意味着模板中应出现ItemsPresenter(在**StackPanel所在的位置)。如果要更改ComboBox用于呈现项目的面板,请更改ComboBoxItemsPanel模板。

相关内容

  • 没有找到相关文章

最新更新