Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Window Startup Location from resource dictionary

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?

like image 310
Igal Avatar asked Mar 05 '26 07:03

Igal


1 Answers

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}"
like image 96
Ron.B.I Avatar answered Mar 08 '26 08:03

Ron.B.I



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!