【发布时间】:2011-01-26 16:11:10
【问题描述】:
我正在尝试创建一个简单的 Silverlight 应用程序,该应用程序涉及解析 CSV 文件并将结果显示在 DataGrid 中。我已将我的应用程序配置为解析 CSV 文件以返回一个 List<CSVTransaction>,其中包含具有以下名称的属性:Date、Payee、Category、Memo、Inflow、Outflow。
用户单击按钮选择要解析的文件,此时我希望填充DataGrid 对象。我想我想使用数据绑定,但我似乎不知道如何让数据显示在网格中。
DataGrid 的 XAML 如下所示:
<data:DataGrid IsEnabled="False" x:Name="TransactionsPreview">
<data:DataGrid.Columns>
<data:DataGridTextColumn Header="Date"
Binding="{Binding Date}" />
<data:DataGridTextColumn Header="Payee"
Binding="{Binding Payee}"/>
<data:DataGridTextColumn Header="Category"
Binding="{Binding Category}"/>
<data:DataGridTextColumn Header="Memo"
Binding="{Binding Memo}"/>
<data:DataGridTextColumn Header="Inflow"
Binding="{Binding Inflow}"/>
<data:DataGridTextColumn Header="Outflow"
Binding="{Binding Outflow}"/>
</data:DataGrid.Columns>
</data:DataGrid>
xaml.cs 文件的代码隐藏如下所示:
private void OpenCsvFile_Click(object sender, RoutedEventArgs e)
{
try
{
CsvTransObject csvTO = new CsvTransObject.ParseCSV();
//This returns a List<CsvTransaction> and passes it
//to a method which is supposed to set the DataContext
//for the DataGrid to be equal to the list.
BindCsvTransactions(csvTO.CsvTransactions);
TransactionsPreview.IsEnabled = true;
MessageBox.Show("The CSV file has a valid header and has been loaded successfully.");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void BindCsvTransactions(List<CsvTransaction> listYct)
{
TransactionsPreview.DataContext = listYct;
}
我的想法是将CsvTransaction 属性绑定到XAML 中的每个DataGridTextColumn,然后在运行时将DataGrid 的DataContext 设置为List<CsvTransaction,但这不起作用。
关于我如何处理(或做得更好)的任何想法?
【问题讨论】:
标签: c# .net silverlight data-binding xaml