gpt4 book ai didi

c# - 空异常错误数据表c#

转载 作者:太空宇宙 更新时间:2023-11-03 15:03:12 28 4
gpt4 key购买 nike

我正在尝试为我的行和列分别创建一个标题为的矩阵。当我在 中有两个相同的值时出现错误。但是,当我在 中有两个相同的值时它会起作用。为什么?

错误:

A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll Additional information: 'Set property 'System.Windows.Controls.ItemsControl.ItemsSource' threw an exception.' Line number '15' and line position '48'.

enter image description here

我的 xaml:

 <Window.Resources>
<app:MatrixToDataViewConverter x:Key="MatrixToDataViewConverter"/>
</Window.Resources>
<Grid>
<DataGrid>
<DataGrid.ItemsSource>
<MultiBinding Converter="{StaticResource MatrixToDataViewConverter}">
<Binding Path="ColumnHeaders" ElementName="Result"/>
<Binding Path="RowHeaders" ElementName="Result"/>
<!--<Binding Path="Values"/>-->
</MultiBinding>
</DataGrid.ItemsSource>
</DataGrid>
</Grid>

我的 cs 代码:

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)
{
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] = Vb[j];
}
try
{
InitializeComponent();
}
catch(Exception ex)
{

}
}
}

public class MatrixToDataViewConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
var myDataTable = new DataTable();

string[] columns = values[0] as string[];
string[] rows = values[1] as string[];
myDataTable.Columns.Add("---"); //Upper left corner

foreach (string value in columns)
{
myDataTable.Columns.Add(value); // this is where the error occurs when trying to add the second value if it is the same as the first one.
}

foreach (string value in rows)
{
myDataTable.Rows.Add(value);
}

return myDataTable.DefaultView;
}

public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}

最佳答案

我认为错误在这里:

public string[] RowHeaders { get; set;  }
public string[] ColumnHeaders { get; set; }

如果您不在 ResultPopUp 的构造函数中创建数组,则 RowHeaders 和 ColumnHeaders 将为 null。

关于c# - 空异常错误数据表c#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45073673/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com