在运行时将StackPanel添加到WPF DataGrid

问题描述:

我有一个Datagrid,它是使用以下命令从传入数据动态构建的:

I have a Datagrid that is being built dynamically from incoming data using the following:

TableData = JObject.Parse(File.ReadAllText(@"Datainfo.json"));
var listCols = new List<DataColumn>();
var rawData = new DataTable();
foreach (dynamic item in TableData.data)
{
    string columnName = item.Column;
    var column = new DataColumn(columnName);
    string DataType = item.DataType;
    if (DataType == "Int" )
    {
        column.DataType = Type.GetType("System.Int32");
    }
    else
    {
        column.DataType = Type.GetType("System.String");
    }
    column.Unique = false;
    column.AllowDBNull = true;
    column.AutoIncrement = false;
    listCols.Add(column);
    rawData.Columns.Add(column);
}

然后我将其推入DataGrid,如下所示:

Then I am pushing it to the DataGrid like this:

    DataTable ETL = null;
    ETL = rawData;
    ETL.DefaultView.AllowEdit = true;
    DataGridView.DataContext = ETL;

我想向每个列添加一个StackPanel,其中包括用于列名的TextBox和一个下拉菜单中包含各种数据类型。然后,它将具有应用/取消选项。

I would like to add a StackPanel to each column that includes a TextBox for the Column Name, and a dropdown that has various datatypes in it. It would then have a Apply / Cancel option.

我一直在尝试采用类似的解决方案将扩展器添加到网格中,但是我不知道如何应用它到DataGrid,因为它没有 .Children() .Add()到。
https://www.codeproject.com/Questions/877973/How-Do-I-Add-A-Stackpanel-To-An-Expander-Header-Vi

I have been trying to follow similar solutions for adding Expanders to Grids, but I can't figure out how to apply it to a DataGrid since it doesn't have a .Children() to .Add() to. https://www.codeproject.com/Questions/877973/How-Do-I-Add-A-Stackpanel-To-An-Expander-Header-Vi

有没有一种方法可以解决我的要求?我还尝试隐藏标题行并控制前两行,但是当我尝试将标题加载到Int列时,出现错误,因此显然不是一个好主意。

Is there a way to do what I am asking? I also tried to hide the header row and control the first two rows, but then when I tried loading up the header into a Int column, I get an error, so obviously that isn't a good idea.

如果要将其添加到列标题中,则必须将其添加为HeaderTemplate。
可以说这是XAML ...

If you are adding this to the column header than you must add it as a HeaderTemplate. Lets say this is the XAML...

<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"
        xmlns:local="clr-namespace:testtestz"
        xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity">
    <Grid>
        <ListView>
            <ListView.View>
                <GridView x:Name="myGrid">
                    <GridViewColumn Header="Id"/>
                    <GridViewColumn Header="Name"/>
                    <GridViewColumn Header="Date"/>
                </GridView>
            </ListView.View>
        </ListView>
    </Grid>
</Window>

然后您将在后面的代码中完成此操作...

Then you would do this in the code behind...

using System;
using System.Windows;
using System.Windows.Controls;

namespace testtestz
{ 

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        protected override void OnContentRendered(EventArgs e)
        {
            base.OnContentRendered(e);

            var cBox = new FrameworkElementFactory(typeof(ComboBox));

            myGrid.Columns[0].HeaderTemplate = new DataTemplate() { VisualTree = cBox };
        }
    }
}

这只是一个展示怎么做。当然,您必须考虑一些布局,大小和类似的东西,以使其达到合理的水平,但我希望它会有所帮助。

This is just a showcase of how to do it. Of course you would have to play with some layout, sizes and simillar stuff to get it to a reasonable point but I hope it helps.