Daniil Harik
Daniil Harik

Reputation: 4719

WPF store layout in resources

I have application where default Window's borders switched off

Window tag definition looks like this:

<Window x:Class="TEA.UI.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Title" WindowStyle="None" AllowsTransparency="True" Background="Transparent">

Inside Window tag, there is Grid panel, it contains several Rectangle shapes and few other grids.

It looks like this:

<Grid>
    <!-- WINDOW BACKGROUND -->
    <Rectangle Stroke="#FF214E80" RadiusX="3" RadiusY="3" ClipToBounds="True">
        <Rectangle.Fill>
            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                <GradientStop Color="#FF193C6C" Offset="0"/>
                <GradientStop Color="#FF2A65A4" Offset="1"/>
            </LinearGradientBrush>
        </Rectangle.Fill>
    </Rectangle>
    <!-- // WINDOW BACKGROUND -->

    <!-- HEADER HIGHLIGHT2 -->
    <Rectangle HorizontalAlignment="Stretch" Margin="2,2,2,0" VerticalAlignment="Top" Height="62" RadiusX="2" RadiusY="2">
        <Rectangle.Fill>
            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                <GradientStop Color="#00193C6C" Offset="1"/>
                <GradientStop Color="#4C96ABC3" Offset="0"/>
            </LinearGradientBrush>
        </Rectangle.Fill>
    </Rectangle>
    <!-- // HEADER HIGHLIGHT2 -->
<Grid>
    ....
</Grid>

These rectangle shapes are used in other window dialogs as well.

My questions are:

How would it be possible to store these recatangles inside WPF resource dictionary?

How would I be able to reference them?

Upvotes: 0

Views: 793

Answers (2)

Daniil Harik
Daniil Harik

Reputation: 4719

Actually the solution was quite simple WPF UserControl did the trick for me

Upvotes: 1

Jeff Wain
Jeff Wain

Reputation: 1022

You can create a style in your resource dictionary for these items with setters for each property--one included below.

<Style TargetType="{x:Type Rectangle}" x:Key="WindowBackground">
  <Setter Property="Stroke" Value="#FF214E80"/>
</Style>

Then in your window you can reference the style as such..

<Rectangle Style="{StaticResource WindowBackground}"/>

Upvotes: 0

Related Questions