简体   繁体   English

Windows Wpf 如何使用 c# 动态创建 xaml 网格

[英]Windows Wpf how can I dynamically create xaml grid using c#

Using Visual Studio 2019 + Resharper.使用 Visual Studio 2019 + Resharper。

hey guys, i want to add listviews, that show things from objects, which i get from a list.嘿伙计们,我想添加列表视图,显示对象中的内容,我从列表中获取。 it looks like this, when i code it manually:它看起来像这样,当我手动编码时:

The XAML-Code: XAML 代码:

 <ListView Margin="43,313,642,29" BorderThickness="2" BorderBrush="Red" x:Name="Module1">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <WrapPanel>
                        <TextBlock Text="Modul"/>
                        <TextBlock Text="{Binding Name}" FontWeight="Bold"/>
                    </WrapPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

and the c# code:和 C# 代码:

            List<Module> somename = pPP_2.Components.Modules.Values.Cast<Module>().ToList();

            List<Module> whatevername = new List<Module>(){somename[0]};

            Module1.ItemsSource = whatevername;

The Modules i refer to have several properties, and the {somename[0]} just gets the first of them and puts it in the list.我所指的模块有几个属性, {somename[0]}只是获取其中的第一个并将其放入列表中。

So basically my question: How can i create such xaml code using c#?所以基本上我的问题是:如何使用 c# 创建这样的 xaml 代码? I want to create a listview like this for each element in my list.我想为列表中的每个元素创建一个这样的列表视图。 i Don´t want to create them manually but let the code do it for me.我不想手动创建它们,而是让代码为我做。

thinking about this for days now and would love to get some help here.现在考虑这个问题好几天了,很想在这里得到一些帮助。

Thanks, IRezzet.谢谢,伊雷泽特。

PS You can basically ignore the special list i created there. PS 您基本上可以忽略我在那里创建的特殊列表。 The question should work for every List.这个问题应该适用于每个列表。

You could use an ItemsControl with a ItemTemplate that renders a ListView that has an ItemTemplate rendering the listview-items.您可以将ItemsControl与呈现ListViewItemTemplate一起使用,该ListView具有呈现列表视图项的ItemTemplate If this gets too complex, consider seperating this into a usercontrol to make it more generic.如果这变得太复杂,请考虑将其分离到用户控件中以使其更通用。

The XAML would look something like this: XAML 看起来像这样:

    <ItemsControl x:Name="DynamicGrid">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <ListView BorderThickness="2" ItemsSource="{Binding SubChildren}" BorderBrush="Red">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Name}" FontWeight="Bold"/>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

You will need two Model types for this为此,您将需要两种模型类型

    // And an instance variable
    public ObservableCollection<Outer> Lists { get; } = new ObservableCollection<Outer>();
    public class Outer
    {
        public ObservableCollection<Inner> SubChildren { get; } = new ObservableCollection<Inner>();
    }
    public class Inner
    {
        public string Name { get; set; }
    }

I used this code to seed for testing:我使用此代码进行种子测试:

        for (int i = 0; i < 10; i++)
        {
            var o = new Outer();
            for (int k = 0; k < 10; k++)
            {
                o.SubChildren.Add(new Inner() { Name = "ID: "+k });
            }
            Lists.Add(o);
        }
        DynamicGrid.ItemsSource = Lists;

Doesn´t really work.. The thing is i know how to display stuff from my lists or objects or whatever, but i want to create as many listviews as i have objects inside of my list dynamically.不是真的工作.. 问题是我知道如何从我的列表或对象或其他任何东西显示内容,但我想创建尽可能多的列表视图,因为我的列表中有对象动态。 Means it changes when the number of object changes.意味着当对象的数量发生变化时它会发生变化。 Isn´t there an posibility to do so?没有这样做的可能性吗?

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

相关问题 如何使用XAML和C#从Windows Phone 8中的文件夹创建图库? - How can I create an image gallery from a folder in Windows Phone 8 using XAML and C#? 如何使用XAML / C#向Windows应用商店应用中的控件(框/网格)添加“DropShadow”效果? - How do I add a 'DropShadow' effect to a control (box/grid) in a Windows Store App using XAML/C#? 我无法在Windows 8的C#中创建XAML / DirectX应用程序? - I can't create a XAML/DirectX app in C# for Windows 8? 如何在 XAML 和 C# 中为 WPF 按钮的旋转设置动画? - How can I animate the rotation of a WPF Button in XAML AND in C#? 如何在 C# Windows 窗体应用程序中动态创建网格 - How to Dynamically Create a Grid in C# Windows Form Application 如何为 WPF 中的文本框制作工具提示,使用 C# 而不是 XAML - How can I make a ToolTip for a text box in WPF, Using C# not XAML 如何在 WPF 中使用 C# 组合多个 XAML 文件? - How can i combine multiple XAML files using C# in WPF? 如何在不使用xaml的情况下自动在wpf中创建堆栈面板 - How can I create stackpanels in wpf automatically without using xaml WPF应用程序如何将网格背景xaml转换为c#? - WPF Application how to convert grid background xaml to c#? XAML中的WPF绑定到网格列定义中的C# - WPF binding in XAML to C# in grid columndefenition
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM