I am trying to create a ListBox in which the first item will be a separate button with my team, and after it all the other items from the ItemsSource with a general design will go. It should look something like this. I apologize for my creativity

This is a ListBox

 <ListBox Margin="0,87,0,0" ItemsSource="{Binding Divisions}" ScrollViewer.HorizontalScrollBarVisibility="Disabled" SelectedItem="{Binding SelectedDivision}"> <TextBlock Width="200" Height="50" Margin="0" Padding="0" Text="Add" /> <ListBox.ItemContainerStyle> <Style TargetType="ListBoxItem"> <Setter Property="Margin" Value="5" /> <Setter Property="BorderBrush" Value="Black" /> </Style> </ListBox.ItemContainerStyle> <ListBox.ItemsPanel> <ItemsPanelTemplate> <WrapPanel Orientation="Horizontal" /> </ItemsPanelTemplate> </ListBox.ItemsPanel> <ListBox.ItemTemplate> <DataTemplate> <StackPanel Width="90" Orientation="Vertical"> <TextBlock Width="200" Height="50" Margin="0" Padding="0" Text="{Binding Name}" /> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox> 

But with this implementation, it produces:

An unhandled exception of the type "System.InvalidOperationException" in PresentationFramework.dll Additional Information: The Items family must be empty before using the ItemsSource property.

But how to implement a different idea I have not.

  • Your problem is that you add a TextBlock to the ListBox , which means adding a new Item to its collection, and then you try to set an ItemsSource and then you have a conflict, because ListBox not empty, but you need to do the binding. So the TextBlock clearly superfluous here. как реализовать по другому - for example . That is, make UserControl with the desired design, create a ViewModel for them, well, associate everything with an associated collection. - EvgeniyZ
  • move this first binding <TextBlock ... Text="Add" /> to the top of the Divisions collection - Alias

0