从列表中填充WrapPanel

akr*_*nov 3 c# wpf xaml binding wrappanel

有没有办法动态地将列表按钮绑定到一个WrapPanel以及它们的事件?

Kyl*_*ndo 7

我不太确定这对你想要做的事情是否正确,但听起来非常相似:

上面链接中的XAML如下:

<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>
Run Code Online (Sandbox Code Playgroud)

  • 我是个白痴.对于任何刚接触WPF并希望避免混淆的人,ItemsControl都有一个属性(ItemsSource),可以在运行时分配,而不是通过关联VM上的属性分配.据推测,这也意味着添加属性`ItemsSource ="{Binding MyList}"`可能会起作用...... (2认同)