【问题标题】:Can't set properly my Dialog Window size and placement target无法正确设置我的对话框窗口大小和放置目标
【发布时间】:2019-06-12 20:35:52
【问题描述】:

我已经构建了一个对话框窗口,我计划在整个应用程序中使用它而不是使用消息框。每当我需要使用它时,我都会在我正在使用此语法的 Window 后面的代码中调用它:

public void ShowDialogWindow(object sender, DialogEventArgs e)
{
    DialogWindow dialog = new DialogWindow(e.MessageToShow, DialogType.Error, ButtonsType.OkOnly, this.ActualWidth, this.ActualHeight, this);
    dialog.ShowDialog();
}

这是我的对话框窗口的构造函数

public DialogWindow(string messageToDisplay, DialogType dialog, ButtonsType buttons, double width, double height, object Owner)
{
    InitializeComponent();
    this.DataContext = this;
    this.Owner = Owner as Window;
    AWidth = width;
    AHeight = height;
    -----
}

这是 xaml 中的开始窗口标记

<Window x:Class="DialogWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        ---
        mc:Ignorable="d" WindowStyle="None" 
        WindowStartupLocation="CenterOwner"
        AllowsTransparency="True" 
        Width="{Binding AWidth}" Height="{Binding AHeight}" 
        MinHeight="720" MinWidth="1080">

现在我的问题是。当我在所有者最小化(设置 MinWidth = 1080 和 MinHeght = 720)时调用此对话框时,对话框“有点”适合(Window 和 DialogWindow 的 ActualWidth 和 Actual Height 相同,但在视觉上 DialogWindow 似乎有点大比所有者)

但是当我全屏时,它会发生这种情况:

ActualHeight 不仅与属性 AHeight 不同(正确设置为 Owner 的 ActualHeight),而且根本不在 Owner 窗口的中心,而是在我的第二个屏幕上溢出。这是什么原因造成的,我该如何解决?

【问题讨论】:

  • “当所有者最小化时” - 最小化 代表“最小尺寸”吗?截图上没有minimized。如果要绑定ActualHeigt,那就是possible,一定要了解difference between them
  • @Sinatr 在第一个屏幕截图中我剪切了我的桌面。它在不是全屏的意义上被最小化并获得我设置的 MinHeight 和 MiniWidth。我会检查那些答案,看看我是否弄错了。
  • 你不需要绑定,因为你传递了owner参数(检查guidelines命名顺便说一句)你可以在构造函数中访问它的ActualHeight和宽度。

标签: c# wpf window


【解决方案1】:

所以我将发布它解决了我的问题的内容,但我完全不知道为什么会发生这种情况,所以如果有人有更好的答案,我会检查它是否被接受。 为了解决我的问题,我删除了宽度和高度的绑定,并简单地添加到 DialogWindow 的构造函数中

this.Width = OwnerActualWidth;
this.Height = OwnerActualHeight;

因为我在输入中传递了这些参数

【讨论】:

    猜你喜欢
    • 2018-12-18
    • 2016-01-18
    • 1970-01-01
    • 1970-01-01
    • 2011-01-27
    • 2016-04-02
    • 2011-07-21
    • 1970-01-01
    • 2016-09-08
    相关资源
    最近更新 更多