【问题标题】:Changing the start up location of a WPF window in XAML to Bottom-Right将 XAML 中 WPF 窗口的启动位置更改为右下角
【发布时间】:2016-06-23 13:58:31
【问题描述】:

是否可以在 XAML 中将 WPF 窗口的手动启动位置设置为在屏幕的右下角角开始?我可以在代码隐藏中做到这一点,但是当窗口打开时,它会在手动位置(屏幕的左中)弹出,然后它会跳到屏幕的右下角,看起来不太好。

我可以使用以下编码在 XAML 的左上角设置位置:

<Window x:Class="WpfApplication1.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="Window1" 
    Height="500" Width="500"
    WindowStartupLocation="Manual" 
    Left="0" Top="0">
</Window>

我可以使用 XAML 编码对屏幕的右下角执行相同的操作以适应任何屏幕尺寸吗?

【问题讨论】:

  • 不确定是否只能在 XAML 中完成,但您始终可以隐藏窗口,然后在将其移动到正确位置后显示它以避免“跳转”。
  • @NemanjaBanda 这是个好主意!我什至没有想到 ;P 我将如何隐藏窗口?我使用了this.Visibility = Visibility.Hidden,但它给了我一个错误。
  • 你在使用什么事件?如果我将 Visibility="Collapsed" 放在 XAML 中,然后将 this.Visibility = System.Windows.Visibility.Visible 放在 ContentRendered 事件中,对我来说效果很好。
  • @NemanjaBanda 感谢您的努力,但 Gopichandar 的解决方案对我有用,我不必再隐藏它了。 :)
  • 太好了,没问题,知道不止一种方法做某事永远不会有什么坏处 :)

标签: c# wpf xaml


【解决方案1】:

我认为无法通过xaml 进行设置。但是,您甚至可以在加载不会从手动位置跳转的窗口之前进行设置。

public MainWindow()
{
    InitializeComponent();
    Left = System.Windows.SystemParameters.WorkArea.Width - Width;
    Top = System.Windows.SystemParameters.WorkArea.Height - Height;
}

【讨论】:

    【解决方案2】:

    在XAML 中设置WindowStartupLocation="Manual"。

    然后在Window_Loaded中设置位置like,

    private void Window_Loaded(object sender, RoutedEventArgs e)
     {
        var desktopWorkingArea = System.Windows.SystemParameters.WorkArea;
        this.Left = desktopWorkingArea.Right - this.Width;
        this.Top = desktopWorkingArea.Bottom - this.Height;
     }
    

    【讨论】:

    • 感谢您的回答,但正如我在原始问题中提到的那样,我已经尝试过了,窗口从手动位置跳转到屏幕的右下角。看起来不太好看。
    猜你喜欢
    • 2012-09-17
    • 2020-11-14
    • 2010-12-05
    • 2010-12-05
    • 2020-02-07
    • 2011-11-29
    • 1970-01-01
    • 2017-01-27
    • 2013-04-22
    相关资源
    最近更新 更多