我有一个DataTable,我想将第一行和第一行更改为RowHeaders和ColumnHeaders中我在方法'PopUp'中设置的元素。但是,当我运行程序时,DataTable为空。为什么?
cs-file:
namespace myNameSpace
{
/// <summary>
/// Interaction logic for PopUp.xaml
/// </summary>
public partial class PopUp : Window
{
public string[] RowHeaders { get; set; }
public string[] ColumnHeaders { get; set; }
public ResultPopUp(Array[] Result, List<int> Vb1, List<int> Vb)
{
InitializeComponent();
RowHeaders = new string[Vb.Count];
for (int i = 0; i < Vb.Count; i++)
{
RowHeaders[i] = Vb[i];
}
ColumnHeaders = new string[Vb1.Count];
for (int j = 0; j < Vb1.Count; j++)
{
ColumnHeaders[j] = Vb1[j];
}
}
}
public class MatrixToDataViewConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
var myDataTable = new DataTable();
var columns = values[0] as string[];
var rows = values[1] as string[];
myDataTable.Columns.Add("---"); //Upper left corner
foreach (var value in columns)
{
myDataTable.Columns.Add(value);
}
foreach (var value in rows)
{
myDataTable.Rows.Add(value);
}
return myDataTable.DefaultView;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
xaml:
<Grid>
<DataGrid>
<DataGrid.ItemsSource>
<MultiBinding Converter="{StaticResource MatrixToDataViewConverter}">
<Binding Path="ColumnHeaders"/>
<Binding Path="RowHeaders"/>
<Binding Path="Values"/>
</MultiBinding>
</DataGrid.ItemsSource>
</DataGrid>
</Grid>
答案 0 :(得分:0)
我设法自己搞清楚了。问题是源绑定没有提供转换的价值。我只需要在我的xaml代码中将'ElementName="theNameOfMyWindow"'
添加到'<Binding Path="..."/>
,现在它完美无缺。但感谢所有试图帮助我的人,我真的很感激!