【问题标题】:How to set WPF application windowstartuplocation to top right corner using xaml?如何使用 xaml 将 WPF 应用程序窗口启动位置设置为右上角?
【发布时间】:2012-09-17 09:45:21
【问题描述】:

我只想将 windowstartuplocation 设置为桌面的右上角。现在为了澄清一些事情,我看到这个帖子有同样的问题:

Changing the start up location of a WPF window

现在我不想对他们所指的右或左感到困惑,我希望我的应用程序从右上角开始,其中右指的是我的右侧(不像我的桌面是一个人在看在我和它的右侧)。所以,

1.) 仅将 left 和 top 设置为 0 不是解决方案(将应用程序带到左侧而不是右侧)

2.)我尝试使用 SystemParameters.PrimaryScreenWidth,但我无法在绑定时执行从该值中减去我的应用程序宽度的操作。

有没有一种方法可以在不涉及太多复杂性的情况下做到这一点?

【问题讨论】:

    标签: wpf


    【解决方案1】:

    有没有一种方法可以在不涉及太多复杂性的情况下做到这一点?

    最简单的方法是手动设置起始位置,然后在后面的代码中设置Left 属性:

    <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" 
        Top="0">
    </Window> 
    

    在你的代码后面:

    public Window1()
    {
        InitializeComponent();
        this.Left = SystemParameters.PrimaryScreenWidth - this.Width;
    }
    

    这是我觉得在代码中做这件事的简单性胜过在后面引入代码的任何缺点的地方。

    【讨论】:

    【解决方案2】:

    通过使用 WorkArea.Width 确保将任务栏的宽度考虑在内(当放置在一侧、左侧或右侧时)。通过使用 this.Width 或 this.MaxWidth 确保您的窗口永远不会滑出工作区域,但两者都需要设置它们的值(在 xaml 或后面的代码中)以不为 this.Left 生成值 NaN(非数字) .

    public MainWindow()
        {
            InitializeComponent();
            this.Left = SystemParameters.WorkArea.Width - this.MaxWidth;
        }
    
    <Window
        ... more code here ...
        WindowStartupLocation="Manual" Top="0" MaxWidth="500" >
    

    【讨论】:

      猜你喜欢
      • 2016-06-23
      • 1970-01-01
      • 2011-11-29
      • 2020-11-14
      • 2020-02-07
      • 1970-01-01
      • 1970-01-01
      • 2016-07-23
      • 1970-01-01
      相关资源
      最近更新 更多