0

I have a Combobox with several items (C# Labels because I want to change individual text colors). Within callbacks, when editing some textboxes, I change the color and/or text of the items. When I click on the combobox I see that the items in the list have the correct color/text. However, the color/text changes of the selected item are not directly reflected in the shown combobox text. How can I achieve that?

I tried to set the Text property of the combobox itself: no effect. Also setting the selected item to an empty Label and then set it back to the correct Label has no effect. If I set SelectedIndex to -1 and then back to the correct selected index it works and the shown text is updated, but this triggers the SelectionChanged callback which I do not want. I could first detach the SelectionChanged callback from the combobox and then attach it again, but this is in my opinion very ugly programming.

Maybe I am missing something simple...

Edit I tried binding, following the suggestion of SLak:

List<Label> labels;
MyComboBox.ItemsSource = labels;

The result is still the same. Suppose index 0 is selected. When I change the corresponding Label:

labels[0].Contents = "new content";

then it is not reflected in the selected text of the combobox. When I click the combobox I can see the new text in the unfolded list, but only when I change the selection and then go back to index 0 the new text is shown by the combobox as the selected item. That synchronization should be autmatically.

Jeroen Boschma
  • 317
  • 1
  • 9
  • 1
    You should use databinding instead of setting properties directly. – SLaks Dec 06 '17 at 21:06
  • @SLaks. A quick binding test does not seem to work. I tried code from Roys answer: https://stackoverflow.com/questions/561166/binding-wpf-combobox-to-a-custom-list But color changes to Labels in the List do not appear in the combobox at all. Apart from the fact that the selected item is not displayed... – Jeroen Boschma Dec 06 '17 at 21:52
  • 1
    What exactly did you try, and what happened? WPF binding _does_ work. – SLaks Dec 06 '17 at 21:58
  • @Slaks: I updated my question with the binding approach. Hopefully it also makes clear what functionality I am missing. Thanks for looking into this. – Jeroen Boschma Dec 07 '17 at 19:35

0 Answers0