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>

   
    
    
    
    
    
    
    
    
    
    
    
    
     


Define Double value as resource


   
       

<Window x:Class="Styles.ReuseFontWithResources"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="ReuseFontWithResources" Height="368" Width="378"
    xmlns:s="clr-namespace:System;assembly=mscorlib"
    >
  <Window.Resources>    
    <FontFamily x:Key="ButtonFontFamily">Times New Roman</FontFamily>
    <s:Double x:Key="ButtonFontSize">18</s:Double>
    <FontWeight x:Key="ButtonFontWeight">Bold</FontWeight>    
  </Window.Resources>
  <StackPanel Margin="5">
    <Button Padding="5" Margin="5"
            FontFamily="{StaticResource ButtonFontFamily}"
            FontWeight="{StaticResource ButtonFontWeight}"
            FontSize="{StaticResource ButtonFontSize}" 
              >A Customized Button</Button>
  </StackPanel>
</Window>

   
    
    
    
    
    
    
     


Add a logical resource to the window's resource dictionary


   
    

<Window x:Class="WpfApplication1.MainWindow"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="FunWithResources" Height="207" Width="612" WindowStartupLocation="CenterScreen">
  <Window.Resources>
    <Style x:Key ="MyFunkyStyle">
      <Setter Property ="Control.FontSize" Value ="20"/>
      <Setter Property ="Control.Background">
        <Setter.Value>
          <LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
            <GradientStop Color="Green" Offset="0" />
            <GradientStop Color="Yellow" Offset="0.25" />
          </LinearGradientBrush>
        </Setter.Value>
      </Setter>
    </Style>
    <Style x:Key ="NewFunkyStyle" BasedOn = "{StaticResource MyFunkyStyle}">
      <Setter Property = "Button.Foreground" Value = "Blue"/>
      <Setter Property = "Button.RenderTransform">
        <Setter.Value>
          <RotateTransform Angle = "20"/>
        </Setter.Value>
      </Setter>
    </Style>

  </Window.Resources>
  <StackPanel>
    <Image Name ="companyLogo" Source ="c:image.gif"/>
    <Button Name="btnClickMe" Style ="{StaticResource NewFunkyStyle}" Content = "Click Me"/>
    <Button Name="btnClickMeToo" Style ="{StaticResource NewFunkyStyle}" Content = "Me Too"/>
    <TextBox Name="txtAndMe" Style ="{StaticResource MyFunkyStyle}" Text = "And me!"/>
  </StackPanel>
</Window>

   
    
    
    
     


Font Size Resources


   
       

<StackPanel xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:s="clr-namespace:System;assembly=mscorlib">

    <StackPanel.Resources>
        <s:Double x:Key="fontsizeLarge">18</s:Double>
        <s:Double x:Key="fontsizeSmall">14</s:Double>
    </StackPanel.Resources>

    <Button HorizontalAlignment="Center" 
            VerticalAlignment="Center" 
            Margin="24">
        <Button.FontSize>
            <StaticResource ResourceKey="fontsizeLarge" />
        </Button.FontSize>
        Button with large FontSize
    </Button>

    <Button HorizontalAlignment="Center"
            VerticalAlignment="Center"
            Margin="24"
            FontSize="{StaticResource fontsizeSmall}" >
        Button with small FontSize
    </Button>

</StackPanel>

   
    
    
    
    
    
    
     


Use EnvironmentInfo as Resource


   
       

<StackPanel xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:s="clr-namespace:System;assembly=mscorlib">
    <TextBlock>
        <Label Content="Operating System Version: " />  
        <Label Content="{x:Static s:Environment.OSVersion}" />
        <LineBreak />
        <Label Content=".NET Version: " />
        <Label Content="{x:Static s:Environment.Version}" />
        <LineBreak />
        <Label Content="Machine Name: " />
        <Label Content="{x:Static s:Environment.MachineName}" />
        <LineBreak />
        <Label Content="User Name: " />
        <Label Content="{x:Static s:Environment.UserName}" />
        <LineBreak />
        <Label Content="User Domain Name: " />
        <Label Content="{x:Static s:Environment.UserDomainName}" />
        <LineBreak />
        <Label Content="System Directory: " />
        <Label Content="{x:Static s:Environment.SystemDirectory}" />
        <LineBreak />
        <Label Content="Current Directory: " />
        <Label Content="{x:Static s:Environment.CurrentDirectory}" />
        <LineBreak />
        <Label Content="Command Line: " />
        <Label Content="{x:Static s:Environment.CommandLine}" />
    </TextBlock>
</StackPanel>

   
    
    
    
    
    
    
     


Window level resource

   
       

<Window x:Class="Resources.WindowResource"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Resources" Height="300" Width="300"
    >
  <Window.Resources>
    <ImageBrush x:Key="TileBrush" TileMode="Tile"
                ViewportUnits="Absolute" Viewport="0 0 32 32"
                ImageSource="happyface.jpg" Opacity="0.3"></ImageBrush>
  </Window.Resources>
    <StackPanel Margin="5">
      <Button Background="{StaticResource TileBrush}" Padding="5"
              FontWeight="Bold" FontSize="14" Margin="5"
              >A Tiled Button</Button>
    </StackPanel>
</Window>

   
    
    
    
    
    
    
     


Button style as Window resource


   
       

<Window x:Class="Styles.AutomaticStyles"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="AutomaticStyles" Height="300" Width="300"
    >
  <Window.Resources>
    <Style TargetType="Button">
      <Setter Property="FontFamily" Value="Times New Roman" />
      <Setter Property="FontSize" Value="18" />
      <Setter Property="FontWeight" Value="Bold" />
    </Style>
  </Window.Resources>

  <StackPanel Margin="5">
    <Button Padding="5" Margin="5">Customized Button</Button>
    <Button Padding="5" Margin="5" Style="{x:Null}">A Normal Button</Button>
    <Button Padding="5" Margin="5">Another Customized Button</Button>
  </StackPanel>
</Window>