【问题标题】:How display dynamic table on DataGrid如何在 DataGrid 上显示动态表格
【发布时间】:2020-12-10 14:42:22
【问题描述】:

我无法在数据网格上显示我的表格。此代码返回清除数据网格和 WPF 跟踪日志返回消息:“System.Windows.Data 信息:10:无法使用绑定检索值,并且不存在有效的备用值;改为使用默认值。”。我不明白为什么它不起作用。也许谁知道如何解决这个错误???

MainWindow.xaml.cs

namespace DvdsqlCore
{
    public static class DataGridHelper
    {
        private static void TableDataChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var dataGrid = d as DataGrid;
            var tableData = e.NewValue as TableData;
            if (dataGrid != null && tableData != null)
            {
                dataGrid.Columns.Clear();
                for (int i = 0; i < tableData.ColumnHeaders.Count; i++)
                {
                    DataGridColumn column = new DataGridTextColumn
                    {
                        Binding = new Binding($"Cells[{i}]"),
                        Header = tableData.ColumnHeaders[i]
                    };
                    dataGrid.Columns.Add(column);
                }

                dataGrid.ItemsSource = tableData.Rows;
            }
        }

        public static TableData GetTableData(DependencyObject obj)
        {
            return (TableData)obj.GetValue(TableDataProperty);
        }

        public static void SetTableData(DependencyObject obj, TableData value)
        {
            obj.SetValue(TableDataProperty, value);
        }

        // Using a DependencyProperty as the backing store for TableData.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TableDataProperty =
            DependencyProperty.RegisterAttached("TableData",
                typeof(TableData),
                typeof(DataGridHelper),
                new PropertyMetadata(null, TableDataChanged));
    }

    public class TableDataRow
    {
        public TableDataRow(List<string> cells)
        {
            Cells = cells;
        }

        public List<string> Cells { get; }
    }

    public class TableData
    {
        public TableData(List<string> columnHeaders, List<TableDataRow> rows)
        {
            for (int i = 0; i < rows.Count; i++)
                if (rows[i].Cells.Count != columnHeaders.Count)
                    throw new ArgumentException(nameof(rows));

            ColumnHeaders = columnHeaders;
            Rows = rows;
        }

        public List<string> ColumnHeaders { get; }
        public List<TableDataRow> Rows { get; }
    }

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            TableDataRow tableDataRow1 = new TableDataRow(new List<String>() { "item1", "item2" });
            TableDataRow tableDataRow2 = new TableDataRow(new List<String>() { "item1", "item2" });

            List<String> columns = new List<String>() { "col1","col2"};
            List<TableDataRow> rows = new List<TableDataRow>() { tableDataRow1, tableDataRow2 };

            TableData ResultData = new TableData(columns,rows);

            InitializeComponent();

            sqlDataGrid.DataContext = ResultData;
        }
    }
}

MainWindow.xaml

<Window x:Class="DvdsqlCore.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:h="clr-namespace:DvdsqlCore"
        xmlns:local="clr-namespace:DvdsqlCore"
        xmlns:col="clr-namespace:System.Collections;assembly=mscorlib"
        
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <DataGrid x:Name="sqlDataGrid" 
                  AutoGenerateColumns="False"
                  HorizontalAlignment="Center" 
                  Margin="0,47,0,0" 
                  VerticalAlignment="Top"
                  Width="780"
                  Height="136"
                  h:DataGridHelper.TableData="{Binding ResultData}"
                  >
        </DataGrid>
    </Grid>
</Window>

【问题讨论】:

标签: c# wpf .net-core


【解决方案1】:

感谢 Nawed Nabi Zada 现在它是完美的动态作品。

从 xaml 中删除此字符串 (h:DataGridHelper.TableData="{Binding ResultData}")

并更正了 MainWindow.xaml.cs

namespace DvdsqlCore
{
    public static class DataGridHelper
    {
        private static void TableDataChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var dataGrid = d as DataGrid;
            var tableData = e.NewValue as TableData;
            if (dataGrid != null && tableData != null)
            {
                dataGrid.Columns.Clear();
                for (int i = 0; i < tableData.ColumnHeaders.Count; i++)
                {
                    DataGridColumn column = new DataGridTextColumn
                    {
                        Binding = new Binding($"Cells[{i}]"),
                        Header = tableData.ColumnHeaders[i]
                    };
                    dataGrid.Columns.Add(column);
                }

                dataGrid.ItemsSource = tableData.Rows;
            }
        }

        public static TableData GetTableData(DependencyObject obj)
        {
            return (TableData)obj.GetValue(TableDataProperty);
        }

        public static void SetTableData(DependencyObject obj, TableData value)
        {
            obj.SetValue(TableDataProperty, value);
        }

        // Using a DependencyProperty as the backing store for TableData.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TableDataProperty =
            DependencyProperty.RegisterAttached("TableData",
                typeof(TableData),
                typeof(DataGridHelper),
                new PropertyMetadata(null, TableDataChanged));
    }

    public class TableDataRow
    {
        public TableDataRow(List<string> cells)
        {
            Cells = cells;
        }

        public List<string> Cells { get; }
    }

    public class TableData
    {
        public TableData(List<string> columnHeaders, List<TableDataRow> rows)
        {
            for (int i = 0; i < rows.Count; i++)
                if (rows[i].Cells.Count != columnHeaders.Count)
                    throw new ArgumentException(nameof(rows));

            ColumnHeaders = columnHeaders;
            Rows = rows;
        }

        public List<string> ColumnHeaders { get; }
        public List<TableDataRow> Rows { get; }
    }



    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            TableDataRow tableDataRow1 = new TableDataRow(new List<String>() { "item1", "item2" });
            TableDataRow tableDataRow2 = new TableDataRow(new List<String>() { "item1", "item2" });

            List<String> columns = new List<String>() { "col1","col2"};
            List<TableDataRow> rows = new List<TableDataRow>() { tableDataRow1, tableDataRow2 };

            InitializeComponent();

            DataGridHelper.SetTableData(this.sqlDataGrid, new TableData(columns, rows));
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            TableDataRow tableDataRow1 = new TableDataRow(new List<String>() { "item1", "item2" });
            TableDataRow tableDataRow2 = new TableDataRow(new List<String>() { "item1", "item2" });
            TableDataRow tableDataRow3 = new TableDataRow(new List<String>() { "item3", "item5" });
            TableDataRow tableDataRow4 = new TableDataRow(new List<String>() { "item1", "item2" });
            TableDataRow tableDataRow5 = new TableDataRow(new List<String>() { "item8", "item9" });

            List<String> columns = new List<String>() { "col1", "col2" };
            List<TableDataRow> rows = new List<TableDataRow>() { tableDataRow1, tableDataRow2, tableDataRow3, tableDataRow4, tableDataRow5 };

            DataGridHelper.SetTableData(this.sqlDataGrid, new TableData(columns, rows));
        }
    }
}

【讨论】:

    猜你喜欢
    • 2014-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-23
    • 2017-10-19
    • 1970-01-01
    • 2012-01-04
    相关资源
    最近更新 更多