0

I have a ComboBox that need to be binded to a certain Dictionary value. This is because I could have N tabs (each with an ID) on my app and in each tab I have a ComboBox. Right now I have:

ComboBox cb = new ComboBox()
{
   Width = 200,
   Height = 26,
   Name = "cbRelatorio" + id
};

Binding bindingCb = new Binding();
bindingCb.Source = DataContext;
bindingCb.Path = new PropertyPath("ReportSheet[" + id + "]");
bindingCb.Mode = BindingMode.TwoWay;
bindingCb.ValidatesOnDataErrors = true;
cb.SetBinding(ComboBox.SelectedValueProperty, bindingCb);
sheetPanel.Children.Add(cb);

That "id" is received by the method and is regarding the ID I talked in the beginning. For example, on ReportSheet[1] I'll have the selected value of the combobox on the first tab.

If I select the value, on the combobox this is saved well on the dictionary, but if this respective value is already set, don't show in the combobox on created. Am I missing something here?

For example, If I have a combobox with:

  • Option 1
  • Option 2
  • Option 3

And select one of them is selected well to my ReportSheet[1].

What I want is to define a default value, lets say Option 2:

combobox.itemssource = datacontext;
ReportSheet[1].SelectedValue = "Option 2";

When the combobox is loaded for the first is empty, doesn't have selected the "Option 2".

Andre Roque
  • 503
  • 1
  • 9
  • 31
  • What do you mean by "but if this respective value is already set, don't show in the combobox on created"? I am not able to understand what are you asking for. – AzzamAziz Oct 17 '16 at 13:20
  • I edited my post with an example ;) – Andre Roque Oct 17 '16 at 13:24
  • Looks like what you are binding to isn't an actual value, but a Key Value pair. Take a look here: http://stackoverflow.com/questions/15579885/how-to-get-value-of-selectedvalue-in-combobox-filled-with-dictionary – AzzamAziz Oct 17 '16 at 13:38
  • The question is, I don't want to set the value. I want that to have a binding that when the value is setted the combobox shows that value – Andre Roque Oct 17 '16 at 13:53
  • What does your XAML look like? What item are you binding to to display the value? You may have to bind to DisplayMemberPath. – AzzamAziz Oct 17 '16 at 16:03

0 Answers0