我看到你的绑定可能不正确。从绑定中删除前导斜杠,因为您绑定的不是集合,所以它没有当前项目。
这是我在尝试您的示例时在输出中看到的内容:
System.Windows.Data 错误:40:BindingExpression 路径错误:在“当前集合项”“TransferSchedulesView”(HashCode=19117974)上找不到“属性”。BindingExpression:Path=TransferSchedulesView/; 数据项='主窗口'(名称='');目标元素是'DataGrid'(名称='dataGrid1');目标属性是“DataContext”(类型“对象”)
这是我的工作示例。如果你运行它,你会看到自动生成的列:
<DataGrid AutoGenerateColumns="True"
ItemsSource="{Binding Path=DataView}"
DataContext="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=WpfApplication1:MainWindow, AncestorLevel=1}, Path=TransferSchedulesView}" />
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public TransferSchedulesView TransferSchedulesView { get; set; }
public MainWindow()
{
InitializeComponent();
TransferSchedulesView = new TransferSchedulesView(){DataView = CreateTable()};
}
private static DataTable CreateTable()
{
var dataTable = new DataTable();
dataTable.Columns.Add("aaa");
dataTable.Columns.Add("bbb");
dataTable.Columns.Add("ccc");
dataTable.Rows.Add("sdaasdasd", "dsdsadasdasdsd", "sdasdadsadsadsd");
dataTable.Rows.Add("sdaasdasd", "dsdsadasdasdsd", "sdasdadsadsadsd");
dataTable.Rows.Add("sdaasdasd", "dsdsadasdasdsd", "sdasdadsadsadsd");
return dataTable;
}
}
public class TransferSchedulesView
{
public DataTable DataView { get; set; }
}
}