2012-09-25 41 views
9

我只想將windowstartuplocation設置爲桌面的右上角。我們清楚一些事情的時候,我看到了這個帖子同樣的問題:如何使用xaml將WPF應用程序windowstartuplocation設置爲右上角?

Changing the start up location of a WPF window

現在我不想混淆什麼他們指的是向左或向右,我想我的應用程序啓動右上角,右側是我的右側(不像我的桌面是一個人看着我和他的右側)。因此,

1.)設置左和頂只有0不是解決方案(帶應用程序到左側不對)

2.)我嘗試使用SystemParameters.PrimaryScreenWidth,但我不能執行減法操作t在綁定時從此值開始我的應用程序的寬度。

有沒有辦法我可以做到這一點,而不會變得複雜?

回答

16

有沒有辦法我可以做到這一點,而不會變得複雜?

最簡單的方法是設置你的起始位置手動,然後設置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

對於誰想要它打開左邊的人:http://stackoverflow.com/questions/1545258/changing-the-start-up-location-of-a-wpf-window – 2014-10-19 10:53:05

+0

設置「WindowStartupLocation =」手動「是這個工作的關鍵。 – WhoCares

0

通過使用WorkArea.Width確保考慮任務欄的寬度(放置在一側,左側或右側時)。通過使用this.Width或this.MaxWidth確保您的窗口永遠不會滑到工作區域外,但都要求將它們的值設置爲(在xaml中或在代碼後面),以便不會爲此生成值NaN(不是數字).Left 。

public MainWindow() 
    { 
     InitializeComponent(); 
     this.Left = SystemParameters.WorkArea.Width - this.MaxWidth; 
    } 

<Window 
    ... more code here ... 
    WindowStartupLocation="Manual" Top="0" MaxWidth="500" > 
相關問題