2017-11-10 2 views
0

私はwpfでカスタムメッセージボックスを作成しました。メインウィンドウ内の子ウィンドウの位置を設定する(親)

カスタムメッセージボックスビューXAML:私のメインウィンドウ(親)ユーザーがボタンをクリックしたときに、私はボタンから呼び出しの例として、メッセージボックスウィンドウWPFこのカスタムを示してから、

<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> 

ときにそれを*

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

場合によっては設定することができない場合もあれば、設定したものもありますが動作しません。 誰かが正しく設定するのに手伝ってもらえますか?

+0

をお試しください:https://stackoverflow.com/questions/2446602/wpf-set-dialog-windowメインウィンドウとの相対的な位置づけ私は一見を持ってフィードバックを返します。 – user1624552

+1

もちろん静的メソッドでthis.Widthにアクセスすることはできません。あなたのウィンドウで行うべきShowメソッドは何ですか? – mm8

答えて

1

私は、問題は、子ウィンドウの幅、高さはloadイベントの後に知ることができないということだと思います。この

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; 
    } 
+0

さて、あなたはいくつかのことを忘れました:場合によっては、 "parent.Top +"とあなたが指定したものを続けて追加する必要があります。他のケースでは、 "parent.Left +"も忘れてしまいました。結果をthis.Topとthis.Leftに割り当てる前に、これは必要です。そうでない場合、動作しません。また、MessageBoxLocation.TopCenterの場合は、this.Leftがparent.Width - this.Widthではなくparent.Width + this.Widthの場合に間違いがあります。だからあなたのポストでこれらのことを修正してください。 – user1624552

+0

また、親ウィンドウ(カスタムメッセージボックスウィンドウが含まれている)がカスタムサイズ(最大化されていない)を持つ場合、すべてのケースで完全に機能していると言わなければなりません。親ウィンドウが最大化されている場合、それは機能していません。この場合、メッセージボックスウィンドウが開いているときは常に最後の位置に表示されます。親ウィンドウが最大化されているときに何が起きているのか?私は、親ウィンドウが最大化されているとき、トップと左の座標が更新されていないので、親ウィンドウをメッセージボックスに渡すときに、画面上に自分自身を配置するときに、私はこのために別のスレッドを開きます – user1624552

+0

また、幅と高さを使用するのを見ましたが、ActualWidthとActualHeightをそれぞれ使用しています。結果は同じですが、違いはありませんので、私は違いが何であるか知りたいと思います。あなたは知っていますか?最初のコメントで私があなたに言ったことを訂正してください。私はあなたの答えを受け入れます。 – user1624552

関連する問題