Windows 控件之上的 WPF 控件

问题描述:

我有一个主窗口

<Window >
     <Canvas x:Name="topCanvas" Background="Black">
        <Grid x:Name="mainGrid" Width="{Binding ElementName=topCanvas, Path=ActualWidth}" Height="{Binding ElementName=topCanvas, Path=ActualHeight}">
  </Grid>
        <Canvas 
            Width="{Binding ElementName=topCanvas, Path=ActualWidth}"
            Height="{Binding ElementName=topCanvas, Path=ActualHeight}" 
            Name="MessageField" PreviewMouseMove="MessageField_PreviewMouseMove_1"    
            >                
        </Canvas>
    </Canvas>
</Window>

mainGrid 用于承载其他控件.基本上它有一个选项卡控件,用于托管窗口以及选项卡项中的 wpf 控件.现在我想要一个弹出控件,当出现时它应该位于所有控件的顶部,wpf 以及 windows 控件.

mainGrid is used to host other controls. Basically it has a tab control which is used to host the windows as well as the wpf controls in the tab item. Now I want a pop up control which when comes up should come on top of all the controls, wpf as well as windows control.

现在我有一个用户控件,我可以将其用作弹出窗口,但该控件的问题是,它没有出现在 Windows 控件的顶部.它位于 wpf 控件之上.

Now I have a user control which I am able to use as pop-up but the problem with the control is, it is not coming on the top of the Windowscontrols. It comes on top of wpf controls.

 MoveableMessageBox userControl = new MoveableMessageBox();
            System.Windows.Controls.Canvas.SetZIndex(userControl, (int)1);
            MessageField.Children.Add(userControl);

请建议我如何在 windows 控件的顶部制作此控件.

Kindly suggest how can I make this control on the top of windows control as well.

听起来您只想要一个自定义对话框窗口.幸运的是,在 WPF 中创建这些非常简单.只需像这样扩展 Window 类:

It sounds like you just want a custom Dialog Window. Luckily for you, these are very simple to create in WPF. Just extend the Window class like this:

<Window x:Class="WpfApplication2.Views.PopupWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="PopupWindow" Height="300" Width="300" Background="LightGreen" 
            WindowStyle="None" ResizeMode="NoResize">
    <Grid>
        <TextBlock Text="Message" HorizontalAlignment="Center" 
            VerticalAlignment="Center" />
    </Grid>
</Window>

public partial class PopupWindow : Window
{
    public PopupWindow()
    {
        InitializeComponent();
        PreviewMouseMove += new MouseEventHandler(PopupWindow_PreviewMouseMove);
    }

    private void PopupWindow_PreviewMouseMove(object sender, MouseEventArgs e)
    {
        if (Mouse.LeftButton == MouseButtonState.Pressed) DragMove();
    }
}

DragMove 方法使用户可以移动无边框的Window,该Window 将显示在all 其他Window的顶部代码>s.你可以这样显示:

The DragMove method enables users to move the borderless Window, which will be displayed in top of all other Windows. You can display it like this:

PopupWindow popup = new PopupWindow();
popup.ShowDialog();

当然,这是一个简单的、不完整的例子,我会让你来完成它.例如,此 Window 上没有关闭 Button,因此您必须添加它(在此之前使用 ALT+F4 关闭它).您还需要在 Window 关闭时返回 DialogResult 值,但是您可以从 对话框概述页面在 MSDN 上.

Of course, this is a simple, incomplete example and I'm going to leave it up to you to finish it. For example, there is no close Button on this Window, so you'll have to add that (use ALT+F4 to close it until then). You'll also need to return the DialogResult value when the Window is closed, but you can find out all that you still need to do from the Dialog Boxes Overview page on MSDN.