当我试图模板一个WPF组合框时,遇到了一个奇怪的问题。我有一个第三方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>
样式在控件上正常工作,但序列化给我带来了问题。我怀疑我粘贴的代码段中的'**'有问题。是否有不同的方式来实现组合框样式?
您使用了哪个第三方工具? – 2014-11-24 10:15:39