frankiehf
frankiehf

Reputation: 180

how to change multiple labels based on their name

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.

Upvotes: 1

Views: 879

Answers (2)

frankiehf
frankiehf

Reputation: 180

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

Upvotes: 0

BendEg
BendEg

Reputation: 21098

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?

Upvotes: 1

Related Questions