Update Label of a Window through the Contents of a Usercontrol Combobox

Asked

Viewed 162 times

0

I have :

A Window:

  • Window1 (Mainwindow)

Three Usercontrols:

-Usercontrol1

-Usercontrol2

-Usercontrol3

In window 1 I have a label (label1) and in each of the Usercontrols I have a Combobox.

Each Combobox has 3 options: BLUE, BLACK and WHITE. The same 3 options for each of them.

The goal is when I select an option in a Usercontrol, the mainwindow label updates. If I select Black in Usercontrol1, the Label content shows Black.

If then I go to the USERCONTROL2 and select white, the contents of the label updates to white (basically replaces).

I use C# and WPF.

Anyone can help?

Thank you.

1 answer

0


First you must create the "Selectionchanged" event for your userControl, and when the combobox fires this event, it will be passed to userControl.

See the example of a userControl:

 public partial class UserControl1 : UserControl
 {
    public UserControl1()
    {
        InitializeComponent();
    }
    public event EventHandler ComboXSelectionChanged;
    private void comboWindow2_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (ComboXSelectionChanged != null)
            ComboXSelectionChanged(this, e);
    }

    public object SelectedComboValue
    {
        get { return comboWindow2.SelectedValue; }
        set { comboWindow2.SelectedValue = value; }
    }
 }

UserControl

Then, in your Window you should capture this event and do what you need:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        userControl1.ComboXSelectionChanged += userControl1_ComboXSelectionChanged;
    }

    void userControl1_ComboXSelectionChanged(object sender, EventArgs e)
    {
        labelTexto.Content = userControl1.SelectedComboValue.ToString();
    }


}

Working: inserir a descrição da imagem aqui

Browser other questions tagged

You are not signed in. Login or sign up in order to post.