【发布时间】:2019-05-24 18:11:52
【问题描述】:
我想使用使用不同内容的 ListBoxItems。对于 xaml 中的定义,我使用以下方法。
<Style TargetType="{x:Type ListBoxItem}" x:Key="ExampleBaseListBoxItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<Border>
<ContentPresenter HorizontalAlignment="Stretch" VerticalAlignment="Center" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="ExampleListBoxItem" TargetType="{x:Type ListBoxItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<ListBoxItem Style="{DynamicResource ExampleBaseListBoxItem}">
<TextBox />
</ListBoxItem>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
这很好用,但我有两个问题。
这是在 xaml 中定义的好习惯,还是应该使用 还有什么?
当样式
ExampleBaseListBoxItem 已经存在
请不要担心文本框。我正在寻找一种方法,示例本身仅用于演示目的。
【问题讨论】:
标签: wpf styles code-behind