简体   繁体   English

WrapPanel WPF中的StackPanels

[英]StackPanels in WrapPanel WPF

在此处输入图片说明

Goal is to create Layout like this. 目标是创建这样的布局。

I'm from iOS development and new to WPF development, so my thoughts are going to CollectionViews of StackViews with 4 multi media elements from iOS, but I know that in WPF things are working a far different then in iOS. 我来自iOS开发并且是WPF开发的新手,所以我的想法是转到具有iOS的4种多媒体元素的StackViews的CollectionViews,但是我知道WPF的工作原理与iOS截然不同。 So I'm going to use StackPanels and WrapPanels in WPF, if my solution is WRONG please give me a better one. 因此,我将在WPF中使用StackPanels和WrapPanels,如果我的解决方案不正确,请给我一个更好的解决方案。

<Window x:Class="WPF_MultiViewer.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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WPF_MultiViewer"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <WrapPanel>
            <StackPanel x:Name="MyStackPanel">
                <StackPanel Orientation="Horizontal">
                    <Button Height="100" Width="80">Button1</Button>
                    <Button Height="100" Width="80">Button2</Button>
                </StackPanel>

                <StackPanel Orientation="Horizontal">
                    <Button Height="100" Width="80">Button1</Button>
                    <Button Height="100" Width="80">Button2</Button>
                </StackPanel>
            </StackPanel>
        </WrapPanel>
    </Grid>
</Window>

Result is this 结果是这个 在此处输入图片说明

I know that if I will hardcode it and copy paste 12 times, it will work but problem is that I want to create ONE template and then create it dynamically for example 12 times 4 stack panel in one column. 我知道,如果我要对其进行硬编码并复制粘贴12次,那么它将起作用,但问题是我想创建一个模板,然后动态创建它,例如在一列中动态创建12次4个堆栈面板。 To control and maintain it easily, How can I achieve this in WPF? 为了轻松控制和维护它,如何在WPF中实现它?

To give you an example to work with: 给您一个使用示例:

<ItemsControl x:Name="Stacks" Grid.Column="0" ItemsSource="{Binding example}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Whatever you want"/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Button Content="{Binding Eaxmple}"/>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

In WPF you can use the MVVM (Model-View-ViewModel) Pattern. 在WPF中,您可以使用MVVM(模型-视图-视图模型)模式。 If you want to create elements dynamically you could also use a Library called "Prism" in wpf. 如果要动态创建元素,则还可以在wpf中使用一个名为“ Prism”的库。 Here https://www.c-sharpcorner.com/article/how-to-add-the-dynamic-control-in-to-the-view-from-view-mode/ is a little tutorial how you can create an element at runtime. https://www.c-sharpcorner.com/article/how-to-add-the-dynamic-control-in-to-the-view-from-view-mode/是一个小教程,介绍了如何创建运行时的元素。

For your requirement I would use a Grid and the ViewModel can create the elements dynamically for the defined Rows and Columns. 根据您的要求,我将使用网格,并且ViewModel可以为定义的行和列动态创建元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM