Lucy82
Lucy82

Reputation: 693

C# WPF Background style for all Windows in ResourceDictionary - Target type with relative path?

I'm organizing my projects with folders. So I have put all window files inside "MyWindows" folder, and in another folder called "Styles" I have a ResourceDictionary. Now I want to write a style for background that would apply to all desired windows, like this:

<Style TargetType="Window" x:Key="Ozadje">
        <Setter Property="Background">
            <Setter.Value>
                <LinearGradientBrush EndPoint="0.5,1" MappingMode="RelativeToBoundingBox" StartPoint="0.5,0">
                    <GradientStop Color="#FF66A7B6" Offset="0.997"/>
                    <GradientStop Color="White"/>
                    <GradientStop Color="#FFAEF1F1"/>
                    <GradientStop Color="#FFACEAEA"/>
                    <GradientStop Color="#FF9BF1E6"/>
                    <GradientStop Color="#FFBFD1CF" Offset="0.06"/>
                    <GradientStop Color="#FF6CAAB7" Offset="0.924"/>
                    <GradientStop Color="#FF99BFC4"/>
                </LinearGradientBrush>
            </Setter.Value>
        </Setter>
    </Style>

    <Style TargetType="MyWindows/Window1" BasedOn="{StaticResource {x:Type Window}}" />

I can't set this because all Windows are not in a local: namespace that resource dictionary is. And another problem is that styles for windows must be applied manually for each Window via reference.

Is there anything I can do - besides calling Style="{StaticResource Ozadje}" in Window properties?...I'm curious if I can use a relative path.

P.S.: I'm a beginner in WPF.

Upvotes: 0

Views: 327

Answers (1)

mm8
mm8

Reputation: 169230

Define a namespace mapping for the namespace in which Window1 is defined, e.g.:

<Style xmlns:MyWindows="clr-namespace:MyWindows"
       TargetType="{x:Type MyWindows:Window1}" BasedOn="{StaticResource Ozadje}" />

Upvotes: 1

Related Questions