I need to dynamically generate some combo boxes on a silverlight page.
If I have the following xaml:
<ComboBox Name="Combo1">
<ComboBox.Items>
<ComboBoxItem Name="Option1" Content="Option 1" />
<ComboBoxItem Name="Option2" Content="Option 2" />
</ComboBox.Items>
</ComboBox>
<ComboBox Name="Combo2">
<ComboBox.Items>
<ComboBoxItem Name="Option1" Content="Option 1" />
<ComboBoxItem Name="Option2" Content="Option 2" />
</ComboBox.Items>
</ComboBox>
I get errors saying that the second Option1 name (under Combo2) already exists in the namespace.
I’ve read here that this is a bug in silverlight, but I couldn’t quite understand how to get around it. I need to be able to retrieve the string Option1 from the selected ComboBox item.
Any ideas?
EDIT: I have a custom control that generates some combo boxes. Some sample XAML code would look like this:
<ComboBox Name="Combo1">
<ComboBox.Items>
<ComboBoxItem Name="True" Content="Yes" />
<ComboBoxItem Name="False" Content="No" />
</ComboBox.Items>
</ComboBox>
<ComboBox Name="Combo2">
<ComboBox.Items>
<ComboBoxItem Name="True" Content="Okay" />
<ComboBoxItem Name="False" Content="Not Okay" />
</ComboBox.Items>
</ComboBox>
I want to be able to retrieve the selected ComboBoxItem “Name”, not the “Content”.. However, doing it like this, I will run into errors if any comboboxitem “Name” is not unique (in this control).
You cannot use the same name twice in an XAML/Class file. If you want to dynamically generate a custom combobox, then create a template and use that to dynamically generate the custom combobox or create a custom control that implement combobox.
When doing things like
<ComboBoxItem/>in the xaml, it’s actually calling the constructor of ComboBoxItem to create a ComboBoxItem object. By giving it a name, you can use that to reference the object in behind-code. So imagine if you have two objects with the same name in the behind-code within the same class….the compiler wouldn’t allow you to do that.The link that you referenced is a bug because you should be able to have the same name if the objects are in separate classes, so there shouldn’t be any naming conflict. But in your case, it seems like you’re trying to create two objects with the same name within the same class.