views:

3469

answers:

5

I've got a WPF application with these three types of things...

  • WindowMain
  • UserControlZack
  • WindowModal

UserControlZack1 sits on my WindowMain...

<Window x:Class="WindowMain"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:ProjectName"
        ...
        Name="WindowMain">
    <Grid>
        ...
        <local:UserControlZack x:Name="UserControlZack1" ... />
        ...
    </Grid>
</Window>

UserControlZack1 displays a WindowModal dailog box...

Partial Public Class UserControlZack

   ...

    Private Sub SomeButton_Click(...)
        'instantiate the dialog box and open modally...
        Dim box As WindowModal = New WindowModal()
        box.Owner = ?????
        box.ShowDialog()
        'process data entered by user if dialog box is accepted...
        If (box.DialogResult.GetValueOrDefault = True) Then
            _SomeVar = box.SomeVar
            ...
        End If
    End Sub

End Class

How do I set box.Owner to the correct Window, my running instance of WindowMain?

I cannot use box.Owner = Me.Owner, because "'Owner' is not a member of 'ProjectName.UserControlZack'."

I cannot use box.Owner = Me.Parent, because that returns a Grid, not the Window.

I cannot use box.Owner = WindowMain, because "'WindowMain' is a type and cannot be used as an expression."

A: 

I got it to work by crawling all the way back up through my XAML...

box.Owner = DirectCast(DirectCast(DirectCast(Me.Parent, Grid).Parent, Grid).Parent, Window)

But this seems quite inelegant. Is there a better way?

Zack Peterson
See below on using a RoutedUICommand which I think is the more elegant way. Apologies for the C#, I'm not a VB guy!
Andrew Barrett
A: 

What about changing the name of the window to WindowMain1 or something, and setting the owner to that?

Davy8
"Name 'WindowMain1' is not declared."
Zack Peterson
Ah, I see, didn't read the question carefully enough, you're doing this inside the user control class rather than the main form class
Davy8
+2  A: 

Updating to try and help Greg from the comments. The command works in the main windows menu, the button in the user control and the context menu in the user control.

I'd do it with commands. So have a class Commands.cs something like:

public static class Commands
{
    public static RoutedUICommand TestShowDialogCommand = new RoutedUICommand("Test command", "TestShowDialog", typeof(Commands));
}

Register these in your main window: (you don't need the canshow it defaults to true)

    public Window1()
    {
        InitializeComponent();

        CommandManager.RegisterClassCommandBinding(typeof(System.Windows.Controls.Control),
            new CommandBinding(Commands.TestShowDialogCommand, ShowDialogCommand, CanShowDialogCommand));
    }

    private void ShowDialogCommand(object sender, ExecutedRoutedEventArgs e)
    {
        var box = new Window();
        box.Owner = this;
        box.ShowDialog();

    }

    private void CanShowDialogCommand(object sender, CanExecuteRoutedEventArgs e)
    {
        e.CanExecute = true;
    }

This is my xaml for the main window:

<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:WpfApplication1="clr-namespace:WpfApplication1"
Title="Window1" Height="300" Width="322">
<Grid>
    <StackPanel>
        <Menu>
            <MenuItem Header="Test">
                <MenuItem Header="ShowDialog" Command="{x:Static WpfApplication1:Commands.TestShowDialogCommand}"/>
            </MenuItem>
        </Menu>
        <WpfApplication1:BazUserControl />
    </StackPanel>
</Grid>
</Window>

This is the xaml for my user control (default code behind only)

<UserControl x:Class="WpfApplication1.BazUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:WpfApplication1="clr-namespace:WpfApplication1"
Height="300" Width="300">
<Grid>
    <StackPanel>
        <Button Command="{x:Static WpfApplication1:Commands.TestShowDialogCommand}" Content="ClickMe" ></Button>
        <TextBox>
            <TextBox.ContextMenu>
                <ContextMenu>
                    <MenuItem Header="ShowDialog" Command="{x:Static WpfApplication1:Commands.TestShowDialogCommand}" />
                </ContextMenu>
            </TextBox.ContextMenu>
        </TextBox>
    </StackPanel>
</Grid>
</UserControl>

You could take it a bit further and handle the command in a controller class instead and make it that bit more MVC.

Andrew Barrett
When I use this technique, the CanExecute event isn't called for the reference from the UserControl. CanExecute seems to default to false, disabling the Element that the command is on (a context menuitem, in my case). A MenuItem in the parent window has no problems and is enabled.
Greg D
It has something to do with the command routing. My UserControl does not get focus, but has a context menu. If no Element is selected/focused when I try to view the UserControl's context menu, the command event isn't routed.
Greg D
If you want to post more info in a question I'll see if I can help you out. I use commands throughout the WPF app I'm making, and I'm really loving them at the moment.
Andrew Barrett
Thanks, Andrew. I _think_ I've figured it out, but I won't know 'til I try it. I believe I simply need to set CommandTarget="{Binding ElementName=This}" (This is the x:Name of the UserControl). That ought to source the command event from my usercontrol instead of whatever button has focus. :)
Greg D
Shoot, I tried it, no love. The binding's not legit...
Greg D
Greg, I've updated, hopefully it might help you. Probably will need more info from you though to help you better.
Andrew Barrett
Thanks for the info, Andrew. :) I haven't had much time to look at it today yet. I'll throw a link here if/when I post a question. One diff is that my ContextMenu is on UserControl.ContextMenu and the UserControl is dynamically created/dropped into the Window.
Greg D
I've posted my question to: http://stackoverflow.com/questions/616206/wpf-usercontrol-and-commands-oh-my
Greg D
+1  A: 

To get the top level window your control is in, assuming there is one:

(Window)PresentationSource.FromVisual(this).RootVisual

To get the main window:

Application.Current.MainWindow
Nir
+4  A: 

Try to use .Owner = Window.GetWindow(this)

Martin Moser
Works great, thanks. And is also exactly the answer to this question :-).
gmagana