2012-04-02 23 views
2

我試圖將所有窗口設置爲在屏幕中心打開。 我所有的窗口都使用樣式文件:來自資源字典的窗口啓動位置

<Window.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary Source="../Styles/Mystyles.xaml"/> 
     </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 
</Window.Resources> 

所以我剛插入此屬性的資源字典:

<Style x:Key="windowStyle" TargetType="Window"> 
     <Setter Property="WindowStartupLocation" Value="CenterScreen"/> 
    </Style> 

但是,這是行不通的。我錯過了什麼嗎?

+0

'WindowStartupLocation'是一個CLR屬性,在樣式設置器中只能指定依賴屬性。看到我的[答案](http://stackoverflow.com/questions/10596515/setting-windowstartuplocation-from-resourcedictionary-throws-xamlparseexception/21178555#21178555)。 – 2014-01-17 05:38:44

回答

0

您不需要使用x:Key屬性。你的風格必須是這樣的:

<Style TargetType="{x:Type Window}"> 
    <Setter Property="WindowStartupLocation" Value="CenterScreen"/> 
</Style> 
0

,如果你不想使用隱式樣式(如bniwredyc建議),你必須明確地設置樣式:

<Window **Style="{StaticResource windowStyle}"**> 
<Window.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary Source="../Styles/Mystyles.xaml"/> 
     </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 
</Window.Resources> 
1

不能使用樣式來定義WindowStartupLocation,這是由於它不是依賴屬性。 您可以在資源字典定義靜態資源,你將在你的Windows使用:

<WindowStartupLocation x:Key="StartupLocation">CenterScreen</WindowStartupLocation> 

,然後用它像這樣:

WindowStartupLocation="{DynamicResource StartupLocation}" 
0

盡一切開始在中央屏幕的每個窗口添加此在App.xaml中

<Application.Resources> 
     <WindowStartupLocation x:Key="StartupLocation">CenterScreen</WindowStartupLocation> 
</Application.Resources> 

,並添加窗口標籤此行

WindowStartupLocation="{StaticResource StartupLocation}"