countrymeister
asked on
How to dynamically add checkboxes to a Stack Panel
I have a stack panel and would like to dynamically add check boxes based on a list of items I get from a list.
<StackPanel Grid.Row="5" Height="26" Orientation="Horizontal" HorizontalAlignment="Stret ch" Margin="30,28,0,0" Name="stackPanelPlatform" VerticalAlignment="Stretch " Width="1041" Loaded="stackPanelPlatform _Loaded">
<ItemsControl Name="SPPlatforms" ItemsSource="{Binding platforms}">
<ItemsControl.ItemTemplate >
<DataTemplate>
<CheckBox Content="{Binding Text}" IsChecked="{Binding IsChecked}" Width="200" Name="{Binding Text}"/>
</DataTemplate>
</ItemsControl.ItemTemplat e>
</ItemsControl>
</StackPanel>
Code Behind
private void stackPanelPlatform_Loaded( object sender, RoutedEventArgs e)
{
var platforms = new ObservableCollection<Platf orm>();
Platform pt = new Platform { Text = "ABC", IsChecked = false };
platforms.Add(pt);
pt = new Platform { Text = "XYZ", IsChecked = false };
platforms.Add(pt);
SPPlatforms.ItemsSource = platforms;
}
<StackPanel Grid.Row="5" Height="26" Orientation="Horizontal" HorizontalAlignment="Stret
<ItemsControl Name="SPPlatforms" ItemsSource="{Binding platforms}">
<ItemsControl.ItemTemplate
<DataTemplate>
<CheckBox Content="{Binding Text}" IsChecked="{Binding IsChecked}" Width="200" Name="{Binding Text}"/>
</DataTemplate>
</ItemsControl.ItemTemplat
</ItemsControl>
</StackPanel>
Code Behind
private void stackPanelPlatform_Loaded(
{
var platforms = new ObservableCollection<Platf
Platform pt = new Platform { Text = "ABC", IsChecked = false };
platforms.Add(pt);
pt = new Platform { Text = "XYZ", IsChecked = false };
platforms.Add(pt);
SPPlatforms.ItemsSource = platforms;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.