【发布时间】: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>
【问题讨论】:
-
ResultData 是公共属性还是只是在 MainWindow 构造函数中创建的变量?