Reputation:
I have a combobox whose selection decides the content of a datagrid
<DataGrid Height="100" Margin="10"/>
throug
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;
}
}
}
with
[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;}
}
so in short it changes the items source for the datagrid.
The problem is that I get the following error [translated]:
The element list has to be empty before being used with items source.
This is something I do not understand: I want to be able to change source but keeping elements in the two lists. And use the datagrid to add/edit elements. So the lists can't be empty.
Thanks
Upvotes: 0
Views: 64
Reputation:
My bad, very sorry I had swapped names for combobox and datagrid. This is why the combobox resulted blocked on the comboxbox event
Upvotes: 0
Reputation: 169200
Make sure that you haven't put anything directly inside the <DataGrid>...</DataGrid> element in your XAML markup. Then you will get an error message similar to this if you try to set its ItemsSource property programmatically.
If you are defining the columns explicitly, don't forget the <DataGrid.Columns> node:
<DataGrid>
<DataGrid.Columns>
...
</DataGrid.Columns>
</DataGrid>
Please post the entire markup of your DataGrid if you need any further help.
Upvotes: 2