Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将表格值设置为其他表格中的值_C#_Wpf_Xaml_Mvvm - Fatal编程技术网

C# 将表格值设置为其他表格中的值

C# 将表格值设置为其他表格中的值,c#,wpf,xaml,mvvm,C#,Wpf,Xaml,Mvvm,我有一个表,它可以作为视图模型使用。因此,在我看来,它应该是我绑定的来源。我的目标是基于其他两个表更新此表/视图模型。我的viewModel checkViewModel中要设置的表checklistTable和AnswerTable中各有一个属性 目前,我正在查询每个表中所需的当前元素,并尝试更新viewModel 代码隐藏: //Curent descriptions to be set in the viewModel var currentDescription = (_check.Wh

我有一个表,它可以作为视图模型使用。因此,在我看来,它应该是我绑定的来源。我的目标是基于其他两个表更新此表/视图模型。我的viewModel checkViewModel中要设置的表checklistTable和AnswerTable中各有一个属性

目前,我正在查询每个表中所需的当前元素,并尝试更新viewModel

代码隐藏:

//Curent descriptions to be set in the viewModel
var currentDescription = (_check.Where(s => currentElements.Contains(s.defineId)).Select(s=> s.Description).ToList());

//Current colors to be set in the viewModel
var currentColors = (from current in _answer
                    where current.questionId == currentCheckId && 
                    current.buildingId == currentBuildingId
                    orderby current.dateReported descending
                    select current.backgroundColor).ToList();
检索完这些值后,我尝试更新我的viewModel,这就是问题所在:

for (int i =0 ; i < currentDescription.Count() - 1; i++)
{
  currentViewTable.Description = currentDescription[i];
}
for (int i =0 ; i < currentColors.Count() - 1; i++)
{
    currentViewTable.backgroundColor.Insert(i,currentColors[i]);
}
for(int i=0;i

我得到一个错误:引用未设置为对象的实例。有没有更好的方法来更新我的viewModel,或者关于我做错了什么的提示

我首先建议使用foreach循环,而不是常规的for循环。我发现在处理列表时使用foreach要容易得多,这通常会消除任何明显的“逐个”错误

foreach (var description in currentDescription)
{
    //do something given each element in the list
}
这似乎是一个数据完整性问题,从我所能收集到的信息来看,只要看看您的代码,并给出错误的说明。我建议进入调试模式,查看您生成的列表,查看两个列表中的对象是否有空索引,并相应地处理这些实例

请详细说明:“我有一张作为视图模型的桌子”。。请出示一些xaml。currentViewTable是什么/谁?