首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >主窗口(父窗口)中子窗口的设置位置

主窗口(父窗口)中子窗口的设置位置
EN

Stack Overflow用户
提问于 2017-11-10 15:12:31
回答 1查看 1.2K关注 0票数 0

我在wpf中有一个定制的消息箱。

自定义消息框视图xaml

代码语言:javascript
复制
<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消息框窗口,作为单击按钮时从该按钮调用的一个例子:

代码语言:javascript
复制
var messageBoxResult = WpfMessageBox.Show("Title", "MyMessage",
    MessageBoxButton.YesNo, WpfMessageBox.MessageBoxImage.Warning, this, EnumLocation.TopLeft);

*自定义消息框代码-幕后xaml.cs:

代码语言:javascript
复制
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;
        }
    }
}

在某些情况下,我不知道设置它,而另一些我设置,但它不工作。有人能帮我正确地设置它们吗?

EN

回答 1

Stack Overflow用户

发布于 2017-11-10 15:33:38

尝尝这个

代码语言:javascript
复制
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;
    }
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47225855

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档