简体   繁体   English

WPF 和数据网格数组

[英]WPF and array of datagrids

I'm trying to develop something that - with WinForms - would be a proverbial "piece of cake".我正在尝试开发一些东西 - 使用 WinForms - 将是众所周知的“小菜一碟”。 A dynamic set of data-bound datagrids.一组动态的数据绑定数据网格。 That initializes on application start.在应用程序启动时初始化。 Sometimes there is a need for one, sometimes for five.有时需要一个,有时需要五个。 At first it looked like too much for XAML.起初,它对于 XAML 来说似乎太过分了。 So I'm struggling with it with regular C#.所以我在用普通的 C# 挣扎。 which - with WPF - is insanely unfriendly and I'm hitting the wall again and again.这 - 使用 WPF - 非常不友好,我一次又一次地撞墙。

Or am I doing it all wrong?还是我做错了? Is there any correct way to... "duplicate" / "clone" one datagrid designed and closed with XAML and reuse those clones as a dynamic array?有什么正确的方法可以...“复制”/“克隆”一个用 XAML 设计和关闭的数据网格并将这些克隆重用为动态数组? Whenever I'm looking for a solution to more and more WPF obstacles (ie something as simple (with WinForms) as dynamic row coloring), I find sometimes XAML solutions.每当我正在寻找解决越来越多的 WPF 障碍的解决方案时(即像动态行着色一样简单(使用 WinForms)),我有时会发现 XAML 解决方案。 plain code solutions are extremely rare.纯代码解决方案极为罕见。 even if try to "translate" XAML to regular code, I miss a lot of properties / methods (or maybe they are named differently).即使尝试将 XAML“翻译”为常规代码,我也会错过很多属性/方法(或者它们的名称可能不同)。 anyway - it's like people these days turn to XAML completely.无论如何 - 就像现在人们完全转向 XAML 一样。 are arrays of more complex controls that uncommon?更复杂的控件数组不常见吗? I found some examples of button array bound to some table.我发现了一些绑定到某个表的按钮数组的例子。 and that's pretty much it.仅此而已。 plus it never worked for an array of datagrids...加上它从来没有对数据网格数组起作用......

This is just a prototype of how binding can yield fast results with minimal code written.这只是绑定如何以最少的代码产生快速结果的原型。

<Window x:Class="testtestz.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <ItemsControl ItemsSource="{Binding GridViews}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <ListView ItemsSource="{Binding Data}" BorderBrush="Gray" BorderThickness="1" Margin="5">
                        <ListView.View>
                            <GridView>
                                <GridViewColumn DisplayMemberBinding="{Binding Id}" Header="Id"/>
                                <GridViewColumn DisplayMemberBinding="{Binding Name}" Header="Name"/>
                            </GridView>
                        </ListView.View>
                    </ListView>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </Grid>
</Window>

This is the code behind.这是后面的代码。

using System.Collections.Generic;
using System.Windows;

namespace testtestz
{ 
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            List<object> myData = new List<object>()
            {
                new { Id = 1, Name = "John" },
                new { Id = 2, Name = "Mary" },
                new { Id = 3, Name = "Anna" },
            };

            GridViews.Add(new MyGrid { Data = myData});
            GridViews.Add(new MyGrid { Data = myData });
            GridViews.Add(new MyGrid { Data = myData });

            DataContext = this;
        }

        public List<MyGrid> GridViews { get; } = new List<MyGrid>();
    }

    public class MyGrid
    {
        public IEnumerable<object> Data { get; set; }
    }
}

Keep in mind you can bind almost anything you like so MyGrid class could very well have all the properties needed to create these grids.请记住,您几乎可以绑定任何您喜欢的东西,因此 MyGrid 类可以很好地拥有创建这些网格所需的所有属性。 So for instance, you can have column definitions such as Header texts, column widths and such...因此,例如,您可以拥有列定义,例如标题文本、列宽等...

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

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