在主窗口(父母)内设置子窗口的位置

时间:2017-11-10 15:12:31

标签: c# wpf xaml window wpf-controls

我在wpf中完成了自定义消息框。

自定义消息框视图xaml

<Window x:Class="My.XAML.Controls.Windows.WpfMessageBox"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="WpfMessageBox"  MinHeight="160" 
        MinWidth="420" MaxHeight="750" MaxWidth="750" 
        Background="Transparent" 
        SizeToContent="WidthAndHeight" 
        WindowStartupLocation="Manual"
        ShowInTaskbar="False" ResizeMode="NoResize" 
        WindowStyle="None" Topmost="True">

</Window>

在我的主窗口(父级)中,当用户点击按钮时,我会显示此自定义wpf消息框窗口,作为点击按钮时调用的示例:

var messageBoxResult = WpfMessageBox.Show("Title", "MyMessage",
    MessageBoxButton.YesNo, WpfMessageBox.MessageBoxImage.Warning, this, EnumLocation.TopLeft);

*自定义消息框代码隐藏xaml.cs:

public partial class WpfMessageBox : Window
{
    private WpfMessageBox()
    {
        InitializeComponent();
    }

    public static MessageBoxResult Show(string caption, string text, MessageBoxButton button, MessageBoxImage image, Window parent, EnumLocation location)
    {
        switch (location)
        {
            case MessageBoxLocation.TopLeft:
                this.Top = parent.Top; // works
                this.Left = parent.Left; // works
                break;
            case MessageBoxLocation.TopCenter:
                this.Top = parent.Top;
                this.Left = ? // what goes here?                    
                break;
            case MessageBoxLocation.TopRight:
                this.Top = parent.Top;
                this.Left = (parent.Left + parent.Width) - this.Width; // not working, what goes here?
                break;
            case MessageBoxLocation.MiddleLeft:
                this.Left = parent.Left;
                this.Top = ? // what goes here? 
                break;
            case MessageBoxLocation.MiddleCenter:
                this.WindowStartupLocation = WindowStartupLocation.CenterScreen; // not working so what goes here?
                break;
            case MessageBoxLocation.MiddleRight:
                this.Top = ? // what goes here? 
                this.Left = ? // what goes here? 
                break;
            case MessageBoxLocation.BottomLeft:
                this.Top = (parent.Top + parent.Height) - this.Height; // Not working this
                this.Left = parent.Left;
                break;
            case MessageBoxLocation.BottomCenter:
                this.Top = (parent.Top + parent.Height) - this.Height;    // not working
                this.Left = ? // what goes here?                
                break;
            case MessageBoxLocation.BottomRight:
                this.Top = (parent.Top + parent.Height) - this.Height; // not working
                this.Left = (parent.Left + parent.Width) - this.Width; // not working
                break;

            default:
                break;
        }
    }
}

在某些情况下,我不知道设置它,以及我设置的其他设置但它不起作用。 有人可以帮我正确设置吗?

1 个答案:

答案 0 :(得分:1)

试试这个

switch (location)
    {
        case MessageBoxLocation.TopLeft:
            this.Top = parent.Top; // works
            this.Left = parent.Left; // works
            break;
        case MessageBoxLocation.TopCenter:
            this.Top = parent.Top;
            this.Left = (parent.Width + this.Width) /2; // what goes here?                    
            break;
        case MessageBoxLocation.TopRight:
            this.Top = parent.Top;
            this.Left = parent.Width - this.Width;
            break;
        case MessageBoxLocation.MiddleLeft:
            this.Left = parent.Left;
            this.Top = (parent.Height - this.Height) /2;
            break;
        case MessageBoxLocation.MiddleCenter:
            this.Left = (parent.Width - this.Width) /2 ;
            this.Top = (parent.Height - this.Height) /2;
            break;
        case MessageBoxLocation.MiddleRight:
            this.Top = (parent.Height - this.Height) /2;
            this.Left = parent.Width - this.Width;
            break;
        case MessageBoxLocation.BottomLeft:
            this.Top = parent.Height - this.Height;
            this.Left = parent.Left;
            break;
        case MessageBoxLocation.BottomCenter:
            this.Top = parent.Height - this.Height;   
            this.Left = (parent.Width - this.Width) /2 ;
            break;
        case MessageBoxLocation.BottomRight:
            this.Top = parent.Height - this.Height; 
            this.Left = parent.Width - this.Width;
            break;

        default:
            break;
    }