How to change the text of a textblock based on the selection of a combobox, both created dynamically?

49 Views Asked by At

My code creates dynamically a combobox with differents items and it sets it's price in a textbox. However, if I change the sselection of the combobox it doesn't change the price in the textbox. Do you have any ideas on how to proceed?

I didn't use any XAML, only code behind.

Thanks in advance.

I tried using the SelectionChangedEventArgs but I can manage to connect it with the TextChangedEventArgs.

What I tried doing was using the name of the textbox I want to change (that is already set when I created in code behind) and then change its text but I didn't manage accomplish that.

1

There are 1 best solutions below

0
BBlasz On

I don't know if I understand your problem correctly, but maybe using Tag will solve your problem

private void Foo()
{
    var cb = new ComboBox();
    var tb = new TextBlock();
    cb.SelectionChanged += Cb_SelectionChanged;
    cb.Tag = tb;
}

private void Cb_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    var cb = (ComboBox)sender;
    var tb = (TextBlock)cb.Tag;

    tb.Text = cb.SelectedItem.ToString();
}