我有一个ComboBox,我想用一些定制的样式来设计它。我成功地提供了所需的风格。在风格上,我有以下要素:
我提到的几乎所有的链接都使用了相同的方法。但是,使用这种方法,我无法为ComboBox中的项提供模板。由于某些原因,我正在定义的项目模板没有用于呈现项目。有人能帮我吗?我正在粘贴一个代码示例,以使我的问题陈述清楚(在代码中可能有错误,我只是想要一个想法继续进行)。
<Window.Resources>
<Style x:Key="{x:Type ComboBox}" TargetType="{x:Type ComboBox}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Grid>
<ToggleButton Template="{StaticResource MyToggleButton}"/>
<Popup >
<StackPanel>
<Border/>
<ItemsPresenter/>
</StackPanel>
</Popup>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<ComboBox Style="{StaticResource MyStyle}">
<ComboBox.ItemTemplate>
<DataTemplate>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</Grid>
发布于 2013-02-04 10:29:53
如果您试图为您的ControlTemplate控件提供不同的ComboBoxItem,则需要将ComboBox上的ItemContainerStyle属性设置为与您已经对父控件所做的类似的样式,但对于ComboBoxItem。ItemTemplate定义了一个应用于每个项的数据的DataTemplate,然后注入到ComboBoxItem ControlTemplate中。
<ComboBox Style="{StaticResource MyStyle}">
<ComboBox.ItemContainerStyle>
<Style TargetType="{x:Type ComboBoxItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ComboBoxItem}">
<Border>
<ContentPresenter/> <!--ItemTemplate is injected here-->
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ComboBox.ItemContainerStyle>
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=SomePropertyOnMyDataObject}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
https://stackoverflow.com/questions/14692930
复制相似问题