【发布时间】: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