c# – Wpf DataGrid重复列

前端之家收集整理的这篇文章主要介绍了c# – Wpf DataGrid重复列前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想要一个数据网格,其中包含重复的列,如下图所示.

这只是一个数据网格,并且重复了列.
我们怎么能用wpf windows datagrid做到这一点?

谢谢,

解决方法

我尝试以下列方式实现您的问题.

必须在列中定义一定数量的数据.但是这几个,我们希望它们在DataGrid中定义一个重复列.

假设它是已知的,至少大约是列数和行数.然后是要在DataGrid中设置的数据立方体.以下是我的评论示例.

XAML

<Window x:Class="DataGridDynamicAddCol.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525" 
    WindowStartupLocation="CenterScreen" ContentRendered="Window_ContentRendered">

    <Grid>
        <DataGrid Name="DynamicColumnDataGrid" Loaded="DynamicColumnDataGrid_Loaded" AutoGenerateColumns="False"/>
    </Grid>
</Window>

代码背后

public partial class MainWindow : Window
{        
    // Create class with data
    // Note: Each parameter contains a list of values for it.
    public class Person
    {
        public Person() 
        {
            FirstName = new List<string>();
            SecondName = new List<string>();
            Sample = new List<string>();
        }

        public List<string> FirstName { get; set; }

        public List<string> SecondName { get; set; }

        public List<string> Sample { get; set; }
    }

    // Number of columns
    const Int32 COLS = 3;

    // Number of rows
    const Int32 ROWS = 3;

    // Number repeats
    const Int32 RPTS = 3;

    // Array of headers
    string[] HeadersArray = new string[COLS] 
    { 
        "FirstName","SecondName","Sample"
    };

    // Array of values: Depends on the number of columns and rows
    // Note: The number of repetitions you can specify smaller amounts of data
    // If you specify more,then this place will be empty cells
    string[,] ValuesArray = new string[,] 
    {
        {
            { "Andy","Caufmann","Artist"},{ "Sam","Fisher","Spy"},{ "Ben","Affleck","Actor"}
        },{
            { "Jim","Gordon","Sniper"},{ "Maria","Gray","Cleaner"},{ "Katy","Parry","Artist"}
        },{
            { "Jack","Rider","Hunter"},{ "Sven","Vath","DJ"},{ "Paul","Kalkbrenner","Super DJ"}
        }
    };

    private List<Person> OnePerson;

    public MainWindow()
    {
        InitializeComponent();
    }

    private void Window_ContentRendered(object sender,EventArgs e)
    {
        OnePerson = new List<Person>();

        // Create the columns
        for (int cols = 0; cols < COLS; cols++)
        {
            OnePerson.Add(new Person());
        }

        // Create the rows
        for (int cols = 0; cols < COLS; cols++)
        {
            for (int rows = 0; rows < ROWS; rows++)
            {
                OnePerson[rows].FirstName.Add(ValuesArray[cols,rows,0]);
                OnePerson[rows].SecondName.Add(ValuesArray[cols,1]);
                OnePerson[rows].Sample.Add(ValuesArray[cols,2]);
            }
        }

        DynamicColumnDataGrid.ItemsSource = OnePerson;
    }

    private void DynamicColumnDataGrid_Loaded(object sender,RoutedEventArgs e)
    {
        // Create dynamically the columns and rows
        for (int rpts = 0; rpts < RPTS; rpts++ )
        {
            for (int cols = 0; cols < COLS; cols++)
            {
                DataGridTextColumn MyTextColumn = new DataGridTextColumn();
                MyTextColumn.Header = HeadersArray[cols];

                // Binding values from HeadersArray
                MyTextColumn.Binding = new Binding(String.Format("{0}[{1}]",new object[]
                    {
                        HeadersArray[cols],rpts
                    }
                ));

                // Add column in DataGrid
                DynamicColumnDataGrid.Columns.Add(MyTextColumn);
            }
        }
    }
}

产量

重复次数 – 3:

重复次数 – 2:

如果指定重复次数大于多维数据集中的重复次数,您将看到一张图片

这是合乎逻辑的,因为相关数据不可用.

注意:要使用绑定viewmodel,您需要确定适当的列表/集合,将它们设置为元素并将它们组合到单个数据立方体中.在我的示例中,静态数据,但您需要确定数据的来源,数据量,然后将它们放在一边并将它们放在数据立方体中.我认为,没有数据立方体,很难实现这种类型的东西(我尝试了不同的选项,使用转换器,直接从数组中绑定等).

原文链接:https://www.f2er.com/csharp/243479.html

猜你在找的C#相关文章