WPF:如何设置UserControl显示的对话框的所有者窗口?

时间:2009-03-03 17:37:23

标签: .net wpf vb.net dialog user-controls

我有一个包含这三类内容的WPF应用程序......

  • WindowMain
  • UserControlZack
  • WindowModal

UserControlZack1位于我的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显示一个WindowModal dailog框...

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

如何将box.Owner设置为正确的Window,我正在运行的WindowMain实例?

我无法使用box.Owner = Me.Owner,因为“'所有者'不是'ProjectName.UserControlZack'的成员。”

我无法使用box.Owner = Me.Parent,因为它返回一个网格,而不是窗口。

我无法使用box.Owner = WindowMain,因为“'WindowMain'是一种类型,不能用作表达式。”

6 个答案:

答案 0 :(得分:123)

尝试使用

.Owner = Window.GetWindow(this)

答案 1 :(得分:40)

要获得控件所在的顶级窗口,假设有一个:

(Window)PresentationSource.FromVisual(this).RootVisual

获取主窗口:

Application.Current.MainWindow

答案 2 :(得分:8)

MyWpfDialog dialog = new MyWpfDialog();

//remember, this is WinForms UserControl and its Handle property is
//actually IntPtr containing Win32 HWND.

new System.Windows.Interop.WindowInteropHelper(dialog).Owner = this.Handle;

dialog.ShowDialog();

答案 3 :(得分:5)

更新以尝试并帮助Greg发表评论。该命令在主窗口菜单,用户控件中的按钮和用户控件中的上下文菜单中工作。

我会用命令来做。所以有一个类Commands.cs类似于:

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

在主窗口中注册这些:(您不需要canshow,默认为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;
    }

这是我主窗口的xaml:

<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>

这是我的用户控件的xaml(仅限默认代码)

<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>

您可以更进一步,并在控制器类中处理该命令,并使其更多一点MVC。

答案 4 :(得分:0)

我通过我的XAML爬回来一路爬行......

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

但这似乎相当不优雅。还有更好的方法吗?

答案 5 :(得分:0)

如何将窗口名称更改为WindowMain1或其他内容,并将所有者设置为?