问题已解决 - 请参阅这篇文章的底部- 我不断收到 nullreferenceexception 并且无法弄清楚原因,更不用说如何解决它了。我有两个组合框,从一个中选择一个值应该用一组相关的值填充另一个。
<CollectionViewSource x:Key="branchesViewSource"
Source="{Binding Path=Branches, Source={StaticResource contactDBDataSet}}" />
<CollectionViewSource x:Key="ranksViewSource"
Source="{Binding Path=RankPath, Source={StaticResource contactDBDataSet}}" />
private void loadBranches()
{
DocMan.ContactDBDataSet contactDBDataSet = ((DocMan.ContactDBDataSet)(this.FindResource("contactDBDataSet")));
// Load data into the table Branches. You can modify this code as needed.
DocMan.ContactDBDataSetTableAdapters.BranchesTableAdapter contactDBDataSetBranchesTableAdapter = new DocMan.ContactDBDataSetTableAdapters.BranchesTableAdapter();
contactDBDataSetBranchesTableAdapter.Fill(contactDBDataSet.Branches);
System.Windows.Data.CollectionViewSource branchesViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("branchesViewSource")));
branchesViewSource.View.MoveCurrentToFirst();
}
以上工作正常并填充第一个组合框。第二个代码块应该填充第二个组合框。
DocMan.ContactDBDataSet contactDBDataSet = ((DocMan.ContactDBDataSet)(this.FindResource("contactDBDataSet")));
// Retrieve branchComboBox SelectedItem
string currentItem = ((DataRowView)branchComboBox.SelectedItem)["Branch"].ToString();
// Load data into table Ranks
switch (currentItem)
{
case "Army":
rankPath = "ArmyRanks";
DocMan.ContactDBDataSetTableAdapters.ArmyRanksTableAdapter contactDBDataSetArmyRanksTableAdapter = new ContactDBDataSetTableAdapters.ArmyRanksTableAdapter();
contactDBDataSetArmyRanksTableAdapter.Fill(contactDBDataSet.ArmyRanks);
break;
case "Navy":
rankPath = "NavyRanks";
DocMan.ContactDBDataSetTableAdapters.NavyRanksTableAdapter contactDBDataSetNavyRanksTableAdapter = new ContactDBDataSetTableAdapters.NavyRanksTableAdapter();
contactDBDataSetNavyRanksTableAdapter.Fill(contactDBDataSet.NavyRanks);
break;
case blahblahblah and more cases
}
// Populate rankComboBox
System.Windows.Data.CollectionViewSource ranksViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("ranksViewSource")));
ranksViewSource.View.MoveCurrentToFirst();
}
nullreferenceexception 发生在最后一行,基本上告诉我ranksViewSource.View 为null。与第一段代码相比,branchesViewSource.View 的类型为 System.Windows.Data.BindingListCollectionView。提前非常感谢任何帮助或见解。
_---- _ ----
我需要创建一个新的绑定并设置绑定的源和路径,然后根据需要应用我的组合框的 SetBinding() 方法。
DocMan.ContactDBDataSet contactDBDataSet = ((DocMan.ContactDBDataSet)(this.FindResource("contactDBDataSet")));
// Create binding
Binding rankBinding = new Binding();
// Retrieve branchComboBox SelectedItem
string currentItem = branchComboBox.SelectedValuePath;
// Load data into table Ranks
switch (currentItem)
{
case "Army":
rankBinding.Path = new PropertyPath("ArmyRanks");
rankBinding.Source = this.FindResource("rankViewSource");
rankComboBox.SetBinding(ComboBox.ItemsSourceProperty, rankBinding);
DocMan.ContactDBDataSetTableAdapters.ArmyRanksTableAdapter contactDBDataSetArmyRanksTableAdapter = new ContactDBDataSetTableAdapters.ArmyRanksTableAdapter();
contactDBDataSetArmyRanksTableAdapter.Fill(contactDBDataSet.ArmyRanks);
// Populate rankComboBox
CollectionViewSource armyRanksViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("rankViewSource")));
armyRanksViewSource.View.MoveCurrentToFirst();