[英]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
与呈现ListView
的ItemTemplate
一起使用,该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.