简体   繁体   中英

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

Using 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:

 <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:

            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.

So basically my question: How can i create such xaml code using c#? 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. 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. If this gets too complex, consider seperating this into a usercontrol to make it more generic.

The XAML would look something like this:

    <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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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