簡體   English   中英

將C#數據表綁定到WPF數據網格時,允許使用哪些列名?

[英]What column names are allowed when binding a C# DataTable to a WPF DataGrid?

我正在編寫一些UI,該UI從另一個庫中獲取C# DataTable ,並嘗試在WPF DataGrid顯示它。 DataTable的列具有特殊字符(例如.[] )時,這將導致綁定錯誤(並向用戶顯示行顯示錯誤或丟失數據)。 我也許可以嘗試替換掉特殊字符,因此我的問題是:

是否有關於列名合法的參考,以及可能導致綁定錯誤的參考?

在這個簡單的代碼中,我使用DataGrid創建了WPF表單,並將其綁定到DataTable

public DataTable Table { get; private set; }

public MainWindow()
{
    // Set up a table
    Table = new DataTable();
    Table.Columns.Add("a");
    Table.Columns.Add("a.1");
    Table.Columns.Add("a[]");
    Table.Rows.Add("1", "2.", "3.");
    InitializeComponent();

    DataContext = this;
}

上面的代碼產生以下綁定錯誤:

System.Windows.Data Error: 40 : 
BindingExpression path error: '[]' property not found on 'object' ''String' (HashCode=-842352753)'. BindingExpression:Path=a[]; DataItem='DataRowView' (HashCode=44777048); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
System.Windows.Data Error: 40 :
BindingExpression path error: '1' property not found on 'object' ''String' (HashCode=-842352753)'. BindingExpression:Path=a.1; DataItem='DataRowView' (HashCode=14413736); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')   

謝謝!

在綁定和查找屬性期間會有一些反射,這意味着您不能指定在反射期間會導致“語法錯誤”的列名。 https://msdn.microsoft.com/zh-CN/library/bb613546(v=vs.110).aspx

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM