Im having a very funny issue in WPF
Im creating a Combobox through code, and adding it to a control.
When I set the Combobox.SelectedItem or Combobox.SelectedIndex or Combobox.SelectedValue I am unable to select another option from the Combox items.
ForeignKeyDisplayAttribute attribute = (ForeignKeyDisplayAttribute)this.GetAttribute(typeof(ForeignKeyDisplayAttribute), property);
if (attribute != null)
{
ForeignKeyDisplayAttribute fkd = attribute;
Type subItemType = fkd.ForeignKeyObject;
contentControl = new ComboBox();
object blankItem = System.Activator.CreateInstance(subItemType, new object[] { });
System.Reflection.MethodInfo method = subItemType.GetMethod("Load", new Type[] { typeof(int) });
object innerValue = method.Invoke(null, new object[] { value });
System.Collections.IList selectedSubItems = (System.Collections.IList)subItemType.GetMethod("Load", Type.EmptyTypes).Invoke(null, new object[] { });
selectedSubItems.Insert(0, blankItem);
((ComboBox)contentControl).SelectedValuePath = fkd.IdField;
((ComboBox)contentControl).DisplayMemberPath = fkd.DescriptionField;
((ComboBox)contentControl).ItemsSource = selectedSubItems;
((ComboBox)contentControl).InvalidateVisual();
// If I use any of the two below lines or SelectedItem then I can't change the value via the UI.
((ComboBox)contentControl).SelectedIndex = this.FindIndex(selectedSubItems, value);
((ComboBox)contentControl).SelectedValue = value;
}
Any idea's as to how I can fix this?
Well found the answer.
Turns out I had incorrectly coded the overridden object Equals method, and it was always returning false.
should have been
Do you have Bindings on those ComboBox items in GUI? Then the simple reason is: setting a value manually in the code behind destroys the binding.
Workarround: Before setting the value manually you can get the binding with the BindingOperations static functions.
Jan