1

i've got a list of comboboxes called cb1,cb2,cb3 etc. and a label lb1 lb2 lb3 next to each combobox . the combobox contains 3 values each, low medium and high, and every time a combobox item is selected, theres a label next to the combobox showing a "value",so "low" shows 25, medium shows 50 etc. i managed to have one single routine that controls all the comboboxes, like this:

private void comboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    ComboBox cb = sender as ComboBox;
    string qr = cb.SelectedItem.ToString();
    switch (qr) {
    case "low":
        lb11.Content = "25";
    break;
    case "medium":
        lb11.Content = "50";
    break;
    case "high":
        lb11.Content = "75";
    break;
}

i need to change a label next to it according to the combobox name, say the combo is called cb22, i need to change label named lb22 etc.

Red
  • 2,728
  • 1
  • 20
  • 22
frankiehf
  • 180
  • 2
  • 14

2 Answers2

1

In your case i would use Binding together with ValueConverter, because processing UI stuff in the code behind is agains the idea of WPF. The aim is to separate UI logic from codebehind.

For more information take a look on SO, for example:

  1. WPF Databinding
  2. WPF Combobox with ItemsSource - What path do I set to access this databinding?
Community
  • 1
  • 1
BendEg
  • 20,098
  • 17
  • 57
  • 131
0

solved,thanks a lot. i dunno if its the proper way to deal with it,but i just declared the label as:

var lb = (Label)this.FindName("label" + cb.Name);

and then i can change the value in the switch case using lb.content

frankiehf
  • 180
  • 2
  • 14
  • @frankieht remember, that this is a kind of solution, but not the way you should work using `WPF`. But good it works :) – BendEg Mar 18 '16 at 12:27