列表框的分组矩形
本文关键字:列表 | 更新日期: 2023-09-27 17:55:48
我有列表框,用户可以对任意数量的项目进行分组。我想在分组项目周围显示一个红色矩形。我尝试使用装饰器,但似乎可以在单个控件上绘制装饰器。有没有更简单的方法来实现它,可能只在 xaml 中。
<ListBox ItemsSource="{Binding MyList, RelativeSource={RelativeSource AncestorType={x:Type Window}}}">
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Border Height="50" Width="100" Background="Yellow">
<TextBlock Text="{Binding item}"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
正如XAMIMAX所建议的那样。您需要为分组项编写样式。请参阅下面的代码。
<ListBox.GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Border BorderBrush="Red" BorderThickness="2" >
<StackPanel>
<ContentPresenter/>
<ItemsPresenter />
</StackPanel>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</ListBox.GroupStyle>