2012-04-02 18 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)を参照してください。 –

答えて

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を定義し、これをスタイルを使用することはできません依存関係プロパティではないためです。 あなたはあなたの窓に使用するリソース・ディクショナリにStaticResourceを定義することができます。

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

とそのようにようにそれを使用します。すべてのセンター画面ですべてのウィンドウは、これを追加起動するようにするには

WindowStartupLocation="{DynamicResource StartupLocation}" 
0

App.xaml

でライン
<Application.Resources> 
     <WindowStartupLocation x:Key="StartupLocation">CenterScreen</WindowStartupLocation> 
</Application.Resources> 

とWindowタグ内でこの行を追加します

WindowStartupLocation="{StaticResource StartupLocation}"