2011-07-08 23 views
0

我們正在嘗試遵循these指導原則。 爲此,我想設置應用程序級別樣式或屬性來設置控件之間的邊距。設置應用程序級別的保證金屬性

我不能通過樣式設置邊距,因爲它要求我輸入目標對象,並且可能會出現這種情況,因爲我不想按照上述說明進行操作。

我能夠在App.xaml.cs

/// <summary> 
    /// Gets the margin to be set all around the dialog 
    /// </summary> 
    public Thickness MarginsAllAroundDialog 
    { 
     get 
     { 
     // returns default margin 
     return new Thickness(7); 
     } 
    } 

做一些吸氣劑性能和作爲保證金設置對話框來設置margin:

<Window x:Class="XXX.Views.MainWindow" 
     x:Name="mainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:prism="http://www.codeplex.com/prism" 
     Title="MainWindow" 
     Margin="{Binding Path=MarginsAllAroundDialog, Source={x:Static Application.Current}}" 
     Height="350" 
     Width="525" 
     WindowState="Maximized"> 

這是正確的方法或者我們通過更簡單的方式達到同樣的目的。

+0

「我無法通過樣式設置頁邊距,因爲它要求我爲目標對象,可能會出現這樣的情況,當我不想按照上述「在這種情況下沒有任何東西阻止你重寫它。如果您明確指定控件上的邊距,則會覆蓋樣式。 –

+0

感謝喬並沒有意識到這一點:-) – Mohit

回答

2

我認爲你的方法已經夠用了,但考慮在XAML中聲明邊距。

您可以在App.xaml定義利潤率資源:

<Application> 
    <Application.Resources> 
     <Thickness x:Key="MarginsAllAroundDialog" Bottom="7" Left="7" Right="7" Top="7" /> 
    </Application.Resources> 
</Application> 

和參考這些資源,而不是使用綁定:

<Window x:Class="XXX.Views.MainWindow" 
     Margin="{StaticResource MarginsAllAroundDialog}"> 
+0

這是一個更清潔的方法,我一直在尋找。 我嘗試使用 做同樣的,但它不會編譯:-( 我試過 – Mohit

+0

如果您希望它適用於所有事情,你需要指定'