![](/img/trans.png)
[英]TwoWay Binding to ListBox SelectedItem on more than one list box in WPF
[英]Binding more than one array to a WPF datagrid
我嘗試使用C#將一些數組綁定到Wpf數據網格中。 我能做的只是將一個數組綁定到數據網格中。 有誰知道如何將多個數組綁定到數據網格中? 我在下面嘗試的代碼不起作用。 數據網格什么都沒有顯示。
我的Datagrid代碼段:
<DataGrid Name="MyDatagrid" Grid.Column="1" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Date" Width="60" Binding="{Binding Date}"/>
<DataGridTextColumn Header="Time" Width="55" Binding="{Binding Time}"/>
<DataGridTextColumn Header="No" Width="69" Binding="{Binding No}"/>
</DataGrid.Columns>
</DataGrid>
C#代碼片段:
string[] Date = {"2-Nov-2012","2-Nov-2012","2-Nov-2012","2-Nov-2012","2-Nov-2012"};
string[] Time={"10:30","10:32","10:35","10:42","10:45"};
int[] No = { 1, 2, 3, 4, 5 };
MyDataGrid.ItemsSource = No;
MyDataGrid.ItemsSource = Date;
MyDataGrid.ItemSource = Time;
您不能將多個數組綁定到同一個數據網格; 但你可以用一個類來達到同樣的目的:
public class Row
{
public string Date { get; set; }
public string Time { get; set; }
public int No { get; set; }
}
使用Linq填充:
string[] Date = {"2-Nov-2012","2-Nov-2012","2-Nov-2012","2-Nov-2012","2-Nov-2012"};
string[] Time={"10:30","10:32","10:35","10:42","10:45"};
int[] No = { 1, 2, 3, 4, 5 };
Row[] rows = Date.Select( (date, index) => new Row {
Date = date,
Time = Time[index],
No = No[index]
}).ToArray();
MyDataGrid.ItemsSource = rows;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.