Search code examples
c#wpfxamlbindingwrappanel

Fill a WrapPanel from a List


Is there any way of dynamically binding a list buttons to a WrapPanel as well as their events?


Solution

  • I'm not too sure if this is correct for what you are wanting to do, but it sounds very similar:

    The XAML from the link above is as follows:

    <ItemsControl x:Name="activitiesControl" Margin="10">
        <ItemsControl.Template>
            <ControlTemplate>
                <WrapPanel  Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" 
                        FlowDirection="LeftToRight" IsItemsHost="true">
                </WrapPanel>
            </ControlTemplate>
        </ItemsControl.Template>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Button Style="{DynamicResource ActionButton}" HorizontalAlignment="Right" Margin="5" 
                    Content="{Binding Value}" Width="200" 
                    Command="{Binding Path=ViewModel.ActionTypeCommand, 
                        RelativeSource={RelativeSource Mode=FindAncestor,     
                    AncestorType=local:CustomerEditView}}" CommandParameter="{Binding Key}"/>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>