0

I have a context menu what is triggered by each list item of listbox. And, I want to create child window when I select a context menu as below:

xaml

<ListBox.ItemTemplate>
    <DataTemplate>
        <StackPanel Width="150" Orientation="Vertical" Margin="15, 5, 15, 5">
            <StackPanel.ContextMenu>
                <ContextMenu FontSize="16">
                    <MenuItem Header="{x:Static localRes:Resources.ID_STRING_SETTING}" Margin="5" Command="Setting_Click"/>
                </ContextMenu>

It is already a sub page of main window. So, I can't find a way how to set MainWindow instance as the owner of new window.

Behind Code

    private void Setting_Click(object sender, RoutedEventArgs e)
    {
        SettingWindow SettingWindow = new SettingWindow();
        SettingWindow.Owner = /* I do not know how to do */
        SettingWindow.Show();
    }
yaho cho
  • 1,779
  • 1
  • 7
  • 19

1 Answers1

1

If your click command handler is in the code behind for your main window, then you need to set

deviceSettingWindow.Owner = this;

https://learn.microsoft.com/en-us/dotnet/api/system.windows.window.owner?view=netframework-4.8

Here's a small example. It includes a button with a handler whose code is in the code behind -

<Window x:Class="MainWindow.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:ChildWindow"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Button Content="Button" HorizontalAlignment="Left" Margin="114,137,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
    </Grid>
</Window>

CodeBehind:

    using System.Windows;
    namespace MainWindow
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }

            private void Button_Click(object sender, RoutedEventArgs e)
            {
                var childWindow = new ChildWindow.Window1();
                childWindow.Owner = this;
                childWindow.Show();
            }
        }
    }

Child Window - just an empty window

<Window x:Class="ChildWindow.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:ChildWindow"
        mc:Ignorable="d"
        Title="Window1" Height="450" Width="800">
    <Grid>

    </Grid>
</Window>

Child Window Code Behind


using System.Windows;

namespace ChildWindow
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

    }
}

In my example, Since you are in the code behind for the MainWindow, this is a reference to the MainWindow, and setting 'childWindow.Owner = this' is setting the childWindow's owner to the MainWindow which is what I believe you want.

One thing that is a little confusing to me is that you are using a Command and a reference to an event handler in the code behind. I'm pretty sure that's not going to work. Commands need to Bind to an ICommand reference - you'll have to implement your own ICommand class, or use one from MVVM Light or another WPF MVVM Framework. Once you've got that you can pass a reference from the parent window through the Command as a CommandParameter. For an example of how to do this, see passing the current Window as a CommandParameter

If you are using an event handler on a control, then that can bind to the event handler implementation in the code behind like in my example. You need to choose one or the other.

If you're able to provide more details on how you're setup, it would make it easier for me to provide input on which way you need to go.

  • 1
    Thanks for your answer. But, The event handler can't be referred from Page's xaml it it is located in a behind code of MainWindow. – yaho cho May 18 '19 at 15:56
  • I'm trying to understand the structure of your application. Can you provide some more details? Where is DeviceSetting_Click method you posted above located? Is it in the code behind for the Window that contains the xaml for the ListBox template you posted? If you can post a small example with more code that would make your context clear that would be really helpful. – Fred Ferenczhalmy May 18 '19 at 23:57
  • Thank you very much.The way of passing the current window as a CommandParameter is exactly the idea what i want. And, I understood what `Commands` need to bind to an `ICommand` reference from you answer. – yaho cho May 19 '19 at 03:00