Grouping rectangle for listbox

96 views Asked by At

I have list box ,user can group any number of items .I want to show a red rectangle around grouped items.I tried using adorners but it seems adorner can be drawn on single control.Is there any simpler way to achieve it ,may be in xaml only.

<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>
1

There are 1 answers

0
Ayyappan Subramanian On

As XAMIMAX suggested. You need to write style for the GroupItem. Refer below code.

<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>