It is very easy but there is a little catch:
To leverage the 'observable collection' feature, one needs to bind to it but there is no property such as ItemsSource
on WrapPanel
.
Solution:
Use an ItemsControl
and set its panel to be a WrapPanel
that hosts items.
XAML
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfApplication1"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title="MainWindow"
Width="525"
Height="350"
mc:Ignorable="d">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="1*" />
</Grid.RowDefinitions>
<Button Grid.Row="0"
Click="Button_Click"
Content="Add toggle" />
<ItemsControl Grid.Row="1" ItemsSource="{Binding}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel IsItemsHost="True" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</Grid>
</Window>
Code
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls.Primitives;
namespace WpfApplication1
{
public partial class MainWindow : Window
{
private readonly ObservableCollection<ToggleButton> _collection;
public MainWindow()
{
InitializeComponent();
_collection = new ObservableCollection<ToggleButton>();
DataContext = _collection;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
var toggleButton = new ToggleButton
{
Content = "Toggle" + _collection.Count
};
_collection.Add(toggleButton);
}
}
}
Note:
Assigning your collection to the DataContext
relieves you to directly deal with your WrapPanel
, the <ItemsControl Grid.Row="1" ItemsSource="{Binding}">
binds to this property by default.