3

I'm trying to set all my windows to open in the center of the screen. All my windows use style file:

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

So I just inserted this property to the resource dictionary:

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

But, it doesn't work. Am I missing something?

Igal
  • 1,084
  • 2
  • 13
  • 33
  • `WindowStartupLocation` is a CLR property, in the style setters can only be specified dependency property. See my [answer](http://stackoverflow.com/questions/10596515/setting-windowstartuplocation-from-resourcedictionary-throws-xamlparseexception/21178555#21178555). – Anatoliy Nikolaev Jan 17 '14 at 05:38
  • Does this answer your question? [Setting WindowStartupLocation from ResourceDictionary throws XamlParseException](https://stackoverflow.com/questions/10596515/setting-windowstartuplocation-from-resourcedictionary-throws-xamlparseexception) – StayOnTarget Sep 16 '22 at 16:51

4 Answers4

1

You cannot use a Style to define WindowStartupLocation, this is due to the fact that it is not a dependency property. You can define a StaticResource in your resource dictionary which you will use in your windows:

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

and then use it like so:

WindowStartupLocation="{DynamicResource StartupLocation}"
Ron.B.I
  • 2,726
  • 1
  • 20
  • 27
0

To make every start every window at Center Screen Add this line in App.xaml

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

and add this line in Window tag

WindowStartupLocation="{StaticResource StartupLocation}"
Yawar Ali
  • 239
  • 3
  • 4
0

You don't need to use x:Key attribute. Your style must look like this:

<Style TargetType="{x:Type Window}">
    <Setter Property="WindowStartupLocation" Value="CenterScreen"/>
</Style> 
bniwredyc
  • 8,649
  • 1
  • 39
  • 52
0

if you dont want to use implicit styles (as bniwredyc suggested) you must set style explicitly:

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