【问题标题】:Setting position of child window within main window (parent)设置子窗口在主窗口(父窗口)中的位置
【发布时间】:2017-11-10 15:12:31
【问题描述】:

我在 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;
        }
    }
}

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

【问题讨论】:

  • 我认为问题是加载事件后无法知道子窗口的宽度、高度:stackoverflow.com/questions/2446602/… 我会看看并回来反馈。
  • 当然不能以静态方法访问 this.Width 。 Show 方法应该如何处理您的窗口...?

标签: c# wpf xaml window wpf-controls


【解决方案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;
    }

【讨论】:

  • 好吧,您忘记了一些事情:在某些情况下,您必须添加“parent.Top +”,然后是您指定的内容。在其他情况下,您也忘记了 "parent.Left +" 。在将结果分配给 this.Top 和 this.Left 之前,这是必要的。如果没有,它将无法正常工作。在 MessageBoxLocation.TopCenter 的情况下你也有一个错误,因为 this.Left 是 parent.Width - this.Width 不是 parent.Width + this.Width。因此,请在您的帖子中更正这些内容。
  • 另外我必须说,当父窗口(其中包含自定义消息框窗口)具有自定义大小(未最大化)时,它在所有情况下都能完美运行。如果父窗口最大化,它就不起作用,在这种情况下,当消息框窗口打开时,它总是显示在最后一个位置。知道父窗口最大化时会发生什么吗?我已经看到,当父窗口最大化时,顶部和左侧坐标没有被更新,因此当将父窗口传递给消息框时,它在屏幕上定位时失败。我将为此打开另一个线程
  • 我也看到你使用 Width 和 Height 但我分别使用 ActualWidth 和 ActualHeight。结果是一样的,没有区别,所以我想知道有什么区别。你知道吗?更正我在第一条评论中所说的内容,我会接受你的回答。
猜你喜欢
  • 2013-04-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-24
相关资源
最近更新 更多