【发布时间】:2017-04-25 04:56:39
【问题描述】:
我有一个组合框,它的选择决定了数据网格的内容
<DataGrid Height="100" Margin="10"/>
通过
private void ComboBox_DropDownClosed(object sender, EventArgs e)
{
string strComboBoxName = (sender as ComboBox).Name;
string strComboBoxSelectedItem = (sender as ComboBox).Text;
int iComboBoxSelectedItem = (sender as ComboBox).SelectedIndex;
Serializers.Logger.WriteLog("ComboBox Dropdown closed " + strComboBoxName + " selected " + strComboBoxSelectedItem);
string strError = string.Empty;
string strNewText = (sender as ComboBox).Text;
dtgFeatures.ItemsSource = null;
switch (iComboBoxSelectedItem)
{
case 0: dtgFeatures.ItemsSource = obcCfgUsers; break;
case 1: dtgFeatures.ItemsSource = obcCfgPartPrograms; break;
default: MessageBox.Show("ComboBox_DropDownClosed: item " + iComboBoxSelectedItem + " not acknowledged"); break;
}
}
}
与
[Serializable]
public class CfgUsers
{
public string ID { get; set;}
public string Username{ get; set;}
public string Password{ get; set;}
public bool IsAdministrator{ get; set;}
//public Image Photo{ get; set;}
}
[Serializable]
public class CfgPartPrograms
{
public string Group{ get; set;}
public string Description{ get; set;}
public string Filename{ get; set;}
public string Notes{ get; set;}
//public Image Picture{ get; set;}
}
所以简而言之,它会更改数据网格的项目源。
问题是我收到以下错误[已翻译]:
元素列表在与项目源一起使用之前必须为空。
这是我不明白的:我希望能够更改源但将元素保留在两个列表中。并使用数据网格添加/编辑元素。 所以列表不能为空。
谢谢
【问题讨论】:
-
你能显示 DataGrid 的完整 XAML 吗?
-
这是数据网格的整个 xaml 代码。你想要整个xaml吗?与数据网格无关
-
这不可能是你在哪里定义名称的所有 DataGrid XAML? dtgFeatures
-
另外,我们可以看看
CfgUsers和CfgPartPrograms的初始化。那里可能有一些事情可能会搞砸你。 -
@Tuco 还有到
标签: c# wpf datagrid observablecollection itemssource