Include delimiter in list

I need to include a separator between items in my ListBoxItems, for example where some items in my item source will be placed below the separator, and some above it.

For instance:

listboxwithaeparator

The above is done by changing the ControlTemplate to a ListBox:

<ScrollViewer> <StackPanel> <ItemsPresenter /> <Separator BorderBrush="Red" /> <ListBoxItem Content=".." ContentTemplate="..." x:Key="helpItem"/> </StackPanel> </ScrollViewer> 

The problem is that "helpItem" is not selectable because it is not part of my ItemsSource.

At present, the choice will be enough

1) So, my first question is how could I associate this item with my ItemsSource or, alternatively, make it available?

In the future I will no longer have more items to be placed in the lower half of my list

2) How to physically place the Separator in a specific place between my items, as if sharing my ItemsPresenter in a logical place?

+6
source share
1 answer

Instead of several ListBox controls, if you can divide your collection into "n" smaller groups depending on how many sexers you need, you can combine them all through CompositeCollection into the same ListBox

So, for example, I will say that I have:

 public partial class MainWindow : Window { public List<string> CollA { get; set; } public List<string> CollB { get; set; } public MainWindow() { InitializeComponent(); CollA = new List<string> {"A", "B", "C"}; CollB = new List<string> {"D", "E", "F"}; DataContext = this; } } 

and I need a separator between CollA and CollB , then my xaml could be:

 <ListBox> <ListBox.Resources> <CollectionViewSource x:Key="CollectionOne" Source="{Binding CollA}" /> <CollectionViewSource x:Key="CollectionTwo" Source="{Binding CollB}" /> </ListBox.Resources> <ListBox.ItemsSource> <CompositeCollection> <CollectionContainer Collection="{Binding Source={StaticResource CollectionOne}}" /> <ListBoxItem HorizontalContentAlignment="Stretch" IsEnabled="False" IsHitTestVisible="False"> <Rectangle Height="2" Fill="Gray" /> </ListBoxItem> <CollectionContainer Collection="{Binding Source={StaticResource CollectionTwo}}" /> </CompositeCollection> </ListBox.ItemsSource> </ListBox> 

which should produce:

enter image description here

Now the elements are functional, and you can associate the SelectedItem with it and work with it as you wish, and also by checking the SelectedItem against the source collection, you can determine which selected element of the source list belongs to.

+8
source

Source: https://habr.com/ru/post/952179/


All Articles