代码之家  ›  专栏  ›  技术社区  ›  Zack Peterson

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

  •  53
  • Zack Peterson  · 技术社区  · 16 年前

    我有一个WPF应用程序,有这三种类型的东西。。。

    • 窗口主
    • 用户控制扎克

    <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日志框。。。

    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 = Me.Owner

    我不能用 box.Owner = Me.Parent ,因为它返回的是网格,而不是窗口。

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

    6 回复  |  直到 9 年前
        1
  •  134
  •   slavoo user3099232    10 年前

    尝试使用

    .Owner = Window.GetWindow(this)
    
        2
  •  46
  •   Nir    16 年前

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

    (Window)PresentationSource.FromVisual(this).RootVisual
    

    要获取主窗口:

    Application.Current.MainWindow
    
        3
  •  8
  •   slavoo user3099232    10 年前
    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();
    
        4
  •  5
  •   Andrew Barrett    16 年前

    更新尝试帮助格雷格从评论。该命令在windows主菜单、用户控件中的按钮和用户控件中的上下文菜单中工作。

    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风格。

        5
  •  0
  •   Zack Peterson    16 年前

    我通过我的XAML一路爬回来让它工作。。。

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

        6
  •  0
  •   Davy8    16 年前

    把窗口的名称改成WindowMain1或者别的什么,然后把所有者设置成那个名称怎么样?

    推荐文章