Update a Window's Label through the Contents of a ComboBox of a UserControl

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.

The goal is when I select an option in a UserControl, the mainWindow label updates. If I select Black in UserControl1, the contents of the Label shows Black.

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

I use C # and WPF.

Can anyone help?

Thank you.

    
asked by anonymous 30.05.2017 / 19:01

1 answer

0

First you must create the "SelectionChanged" event for your userControl, and when the comboBox triggers this event, it will be passed to the 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; }
    }
 }

TheninyourWindowyoushouldcapturethiseventanddowhatyouneed:

publicpartialclassMainWindow:Window{publicMainWindow(){InitializeComponent();userControl1.ComboXSelectionChanged+=userControl1_ComboXSelectionChanged;}voiduserControl1_ComboXSelectionChanged(objectsender,EventArgse){labelTexto.Content=userControl1.SelectedComboValue.ToString();}}

Running:

    
30.05.2017 / 19:42