我在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;
}
}
}在某些情况下,我不知道设置它,而另一些我设置,但它不工作。有人能帮我正确地设置它们吗?
发布于 2017-11-10 15:33:38
尝尝这个
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;
}https://stackoverflow.com/questions/47225855
复制相似问题