TextGeometry as Resource


   
  

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:src="clr-namespace:MyNameSpace.TextGeometryDemo" 
        Title="TextGeometry Demo">
    <Window.Resources>
        <src:TextGeometry x:Key="txtHollow" Text="Hollow"
                          FontFamily="Times New Roman" 
                          FontSize="192" FontWeight="Bold" />
        
        <src:TextGeometry x:Key="txtShadow" Text="Shadow"
                          FontFamily="Times New Roman"
                          FontSize="192" FontWeight="Bold" />
    </Window.Resources>

    <TabControl>
        <TabItem Header="Hollow">
            <Path Stroke="Blue" StrokeThickness="5"
                  Data="{Binding Source={StaticResource txtHollow}, Path=Geometry}" />
        </TabItem>


    </TabControl>
</Window>
//File:Window.xaml.cs
using System;
using System.Globalization;
using System.Windows;
using System.Windows.Media;

namespace MyNameSpace.TextGeometryDemo
{
    public class TextGeometry
    {
        string txt = "";
        FontFamily fntfam = new FontFamily();
        FontStyle fntstyle = FontStyles.Normal;
        FontWeight fntwt = FontWeights.Normal;
        FontStretch fntstr = FontStretches.Normal;
        double emsize = 24;
        Point ptOrigin = new Point(0, 0);

        public string Text
        {
            set { txt = value; }
            get { return txt; }
        }
        public FontFamily FontFamily
        {
            set { fntfam = value; }
            get { return fntfam; }
        }
        public FontStyle FontStyle
        {
            set { fntstyle = value; }
            get { return fntstyle; }
        }
        public FontWeight FontWeight
        {
            set { fntwt = value; }
            get { return fntwt; }
        }
        public FontStretch FontStretch
        {
            set { fntstr = value; }
            get { return fntstr; }
        }
        public double FontSize
        {
            set { emsize = value; }
            get { return emsize; }
        }
        public Point Origin
        {
            set { ptOrigin = value; }
            get { return ptOrigin; }
        }

        public Geometry Geometry
        {
            get
            {
                FormattedText formtxt = new FormattedText(Text, CultureInfo.CurrentCulture, 
                                      FlowDirection.LeftToRight,
                                      new Typeface(FontFamily, FontStyle,FontWeight, FontStretch), 
                                      FontSize, Brushes.Black);

                return formtxt.BuildGeometry(Origin);
            }
        }

        public PathGeometry PathGeometry
        {
            get
            {
                return PathGeometry.CreateFromGeometry(Geometry);
            }
        }

    }
}

   
    
     


Find Control Styles with FindResource()


   
  

<Window x:Class="ControlStyles.Window1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="ControlStyles" Height="300" Width="300">
  <Window.Resources>
    <Style x:Key="StyleA">
      <Setter Property="Control.FontSize" Value="20" />
      <Setter Property="Control.FontWeight" Value="Bold" />
      <Setter Property="Control.BorderThickness" Value="2" />
    </Style>
    <Style x:Key="StyleB">
      <Setter Property="Control.FontSize" Value="10" />
      <Setter Property="Control.FontWeight" Value="Normal" />
      <Setter Property="Control.BorderThickness" Value="1" />
    </Style>

  </Window.Resources>

  <Grid>
    <Button Click="MyClickEvent"
      Style="{StaticResource StyleB}"
      VerticalAlignment="Top"
      HorizontalAlignment="Left"
      Grid.Column="0"
      Grid.ColumnSpan="1"
      Grid.Row="0"
      Grid.RowSpan="1"
      Margin="20"
      Width="75"
      Height="23"
      Name="btnGo">Go</Button>
  </Grid>
</Window>
//File:Window.xaml.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;


namespace ControlStyles
{
    public partial class Window1 : System.Windows.Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void MyClickEvent(object sender, RoutedEventArgs e)
        {
            btnGo.Style = (Style)FindResource("StyleA");
        }
    }
}

   
    
     


Relative pack URI referring to external component for a Resource


   
             
<Window x:Class="UsePackUri.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="UsePackUri" Height="300" Width="300">
    <StackPanel>
      <StackPanel.Resources>
        <ResourceDictionary Source="/PresentationFramework.Luna;v3.0.0.0;31bf3856ad364e35;component/themes/luna.normalcolor.xaml" />
      </StackPanel.Resources>
    
      <Button Content="Luna" />
      <CheckBox Content="Theme" />
    </StackPanel>

</Window>

   
    
    
    
    
    
    
    
    
    
    
    
    
     


Logical Resources


   
       
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  Title="Simple Window" Background="Yellow">
  
<Grid xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Grid.Resources>
    <SolidColorBrush x:Key="backgroundBrush">Yellow</SolidColorBrush>
    <SolidColorBrush x:Key="borderBrush">Red</SolidColorBrush>
  </Grid.Resources>
  <Grid.Background>
    <StaticResource ResourceKey="backgroundBrush"/>
  </Grid.Background>
  <DockPanel>
    <StackPanel DockPanel.Dock="Bottom" Orientation="Horizontal"
      HorizontalAlignment="Center">
      <Button Background="{StaticResource backgroundBrush}"
        BorderBrush="{StaticResource borderBrush}" Margin="5">
        <Image Height="21" Source="c:image.gif"/>
      </Button>
    </StackPanel>
    <ListBox/>
  </DockPanel>
</Grid>

</Window>

   
    
    
    
    
    
    
     


No Logical Resources


   
       

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="Simple Window">
  
  <DockPanel Background="Yellow">
    <StackPanel DockPanel.Dock="Bottom" Orientation="Horizontal"
      HorizontalAlignment="Center">
      <Button Background="Yellow" BorderBrush="Red" Margin="5">
        <Image Height="21" Source="c:image.gif"/>
      </Button>
    </StackPanel>
    <ListBox/>
  </DockPanel>

</Window>

   
    
    
    
    
    
    
     


Load Resource for Application

   
       

<Application x:Class="SimpleStyles.app"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    StartupUri="Window1.xaml"
    >
  <Application.Resources>
    <ResourceDictionary>
      <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="ResourcesShared.xaml" />
        <ResourceDictionary Source="ResourcesButton.xaml" />
      </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
  </Application.Resources>
</Application>

   
    
    
    
    
    
    
     


Dynamic Resource: SystemColors.InactiveCaptionBrushKey


   
       

<StackPanel xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Background="{DynamicResource {x:Static SystemColors.InactiveCaptionBrushKey}}">

    <StackPanel.Resources> 
        <LinearGradientBrush x:Key="dynabrush1" StartPoint="0 0" EndPoint="1 1">
            <LinearGradientBrush.GradientStops>
                <GradientStop Offset="0" Color="{DynamicResource {x:Static SystemColors.ActiveCaptionColorKey}}" />
                <GradientStop Offset="1" Color="{DynamicResource {x:Static SystemColors.InactiveCaptionColorKey}}" />
            </LinearGradientBrush.GradientStops>
        </LinearGradientBrush>
        <SolidColorBrush x:Key="dynabrush2" Color="{DynamicResource {x:Static SystemColors.ActiveCaptionColorKey}}" />
    </StackPanel.Resources>
    <Label HorizontalAlignment="Center"
           FontSize="96"
           Content="Dynamic Resources"
           Background="{StaticResource dynabrush1}"
           Foreground="{StaticResource dynabrush2}" />

</StackPanel>