WPF c# how to add a user control to a panel children dynamically that bubbles/tunnels from UC to Panel

420 views Asked by At

I have created a usercontrol (named UCDataGrid) that I want to add to my Main Window at runtime.

In MainWindow.xaml.cs:

private void GetAnotherUserControl(string sUCName)
{
    var UC_Grid = new UCDataGrid();

    ...

    // Add to 'PositioningGrid' Grid Layout
    PositioningGrid.Children.Add(UC_Grid);

    // Register UC_Grid Name
    PositioningGrid.RegisterName(sUCName, UC_Grid);

    ...
}

In MainWindow.XAML [Structure]:

Grid (named “TopGrid”) -> Grid (named “PostioningGrid”)

The PositioningGrid will hold the children controls/user controls I develop which shall be added dynamically at runtime depending on Business rules.

By adding my user control in this fashion, how can I do bubbling/tunneling when I click on a checkbox in my dynamically added usercontrol?

I want to click the checkbox in my usercontrol (UCDataGrid) and run code from my MainWindow.

I have tried several ways but would like to know if there is some way to notify my MainWindow that the user has clicked on the checkbox.

1

There are 1 answers

0
mm8 On

Handle the CheckBox.Checked routed event in the window:

<Window x:Class="WpfApplication1.Window1"
        ...
        Title="Window7" Height="300" Width="300" CheckBox.Checked="Window_Checked">

Or:

 this.AddHandler(CheckBox.CheckedEvent, new RoutedEventHandler(Window_Checked));

The Checked event will bubble up from the CheckBox to the parent window.