简体   繁体   中英

Calling a method from a user control to a routed event in wpf?

Can anyone help me on this problem
I have a wpf application with one Window and one User Controls.

User Control: InventoryMenu
Window: MenuView

I created a routed event on my InventoryMenu:

NewImage.AddHandler(Image.MouseDownEvent, new RoutedEventHandler(First_Click1));  

Now on my First_Click1 method, I am calling on a method from MenuView

private void First_Click(object sender, RoutedEventArgs e)  
{  
    MenuView menu = new MenuView();  
    menu.showInventoryView();  
} 

Now from MenuView:

public void showInventoryView()
{  
    Inventory inventoryView = new Inventory();  
    ChildView.Children.Clear();
    ChildView.Children.Add(inventoryView);  
    MessageBox.Show("I was called");  
}

The problem is that the method was called but the view was not shown?

Thanks!

You may want to set invertoryView.Visible = true; or inventoryView.SetVisible(true);

There is no Children Property for Window. Window is a ContentControl and has only one Child. You can do it in xaml very easy. Here is some example how you can do it :

<Window x:Class="TestNamespace.MenuView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:TestNamespace"
    Title="MenuView" Height="300" Width="300">
    <local:InventoryMenu/>
</Window>

Where local is your Local Namespace where your UserControl is placed. And now the child of the Window is your UserControl. If you want more than one UserControl in the Window you can say that the Child of Window is a Panel like Grid or StackPanel and place your UserControls in the Panels.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM