3
votes

I have a Simple WPF userControl called "xNavPanel" Contains a panel. I added this control into Winform using "ElementHost". I have Another WPF UserControl called "MainMenuButton" contains textblock.

From Winform; I add many "MainMenuButton" in "xNavPanel" Dynamically in runtime. I have method which do loop over DataTable rows and foreach row adding "MainMenuButton" into "xNavPanel" And Add an event handler to each "MainMenuButton".

The Problem is; Controls doesn't invoke the handlers!

public static void FillMenu(xNavPanel navPanel, DataTable Items)
{
    navPanel.MainPanel.Children.Clear(); //MainPanel is Panel inside UserControl

    MainMenuButton menuItem;
    foreach (DataRow dr in Items.Rows)
    {
        menuItem = SetMenuItem(dr);
        navPanel.MainPanel.Children.Add(menuItem);
    }
}
static MainMenuButton SetMenuItem(DataRow dr)
{
    MainMenuButton menuItem = new MainMenuButton();
    menuItem.Name = "MI_" + dr["FormName"].ToString();
    menuItem.MenuName.Text = dr["Name"].ToString(); //MenuName is TxBlock inside UserControl

    menuItem.MouseUp += new System.Windows.Input.MouseButtonEventHandler(menuItem_MouseUp);
    //menuItem.MouseUp += ((sender, e) => MessageBox.Show((sender as MainMenuButton).Name));

    return menuItem;
}
static void menuItem_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
    MessageBox.Show((sender as MainMenuButton).Name);
}
1

1 Answers

0
votes

I found the problem
The code in this Question is working 100%
the problem was in something else not included in this code.