Use Resources.Add to add static resouce from code


   
  


<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="GradientBrushResourceDemo" Height="300" Width="300">
    <Window.Resources>
        <LinearGradientBrush x:Key="brushGradient"
                             StartPoint="0, 0"
                             EndPoint="1, 1">
            <LinearGradientBrush.GradientStops>
                <GradientStop Offset="0" Color="Black" />
                <GradientStop Offset="0.5" Color="Green" />
                <GradientStop Offset="1" Color="Gold" />
            </LinearGradientBrush.GradientStops>
        </LinearGradientBrush>
    </Window.Resources>
    <StackPanel>
        <TextBlock Margin="{StaticResource thicknessMargin}"
               Foreground="{StaticResource brushGradient}">
            Gradient text
        </TextBlock>
        <TextBlock Margin="{StaticResource thicknessMargin}"
                   Foreground="{StaticResource brushGradient}">
            Of black, green, and gold
        </TextBlock>
        <TextBlock Margin="{StaticResource thicknessMargin}"
                   Foreground="{StaticResource brushGradient}">
            Makes an app pretty,
        </TextBlock>
        <TextBlock Margin="{StaticResource thicknessMargin}"
                   Foreground="{StaticResource brushGradient}">
            Makes an app bold.
        </TextBlock>
    </StackPanel>
</Window>
//File:Window.xaml.cs
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            Resources.Add("thicknessMargin", new Thickness(24, 12, 24, 23));

            InitializeComponent();
        }
    }
}

   
    
     


Load Xaml Resource


   
  
<StackPanel xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">

    <Button Name="MyButton" HorizontalAlignment="Center" Margin="24">Button</Button>

    <Ellipse Width="200" Height="100" Margin="24" Stroke="Red" StrokeThickness="10" />

    <ListBox Width="100" Height="100" Margin="24">
        <ListBoxItem>Sunday</ListBoxItem>
        <ListBoxItem>Monday</ListBoxItem>
        <ListBoxItem>Tuesday</ListBoxItem>
    </ListBox>

</StackPanel>
//File:Window.xaml.cs
using System;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;

namespace MyNameSpace.LoadXamlResource
{
    public class LoadXamlResource : Window
    {

        public LoadXamlResource()
        {
            Title = "Load Xaml Resource";

            Uri uri = new Uri("pack://application:,,,/LoadXamlResource.xml");
            Stream stream = Application.GetResourceStream(uri).Stream;
            FrameworkElement el = XamlReader.Load(stream) as FrameworkElement;
            Content = el;

            Button btn = el.FindName("MyButton") as Button;

            if (btn != null)
                btn.Click += ButtonOnClick;
        }
        void ButtonOnClick(object sender, RoutedEventArgs args)
        {
            Console.WriteLine(args.Source.ToString());
        }
    }
}

   
    
     


Cropped image as Resource

   
  

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="ImageElementExample.CroppedImageExample"
    Title="CroppedImage Example"
    Loaded="PageLoaded">
    <Page.Resources>
       <BitmapImage x:Key="masterImage" UriSource="c:image.jpg" />
       <CroppedBitmap x:Key="croppedImage" Source="{StaticResource masterImage}" SourceRect="30 20 105 50"/>
    </Page.Resources>
    <StackPanel>
         <Image Width="200" Source="{StaticResource masterImage}"/>
         <Image Width="200" Source="{StaticResource croppedImage}"/>
         <Image Width="200">
            <Image.Source>
               <CroppedBitmap Source="{StaticResource croppedImage}" SourceRect="30 0 75 50"/>
            </Image.Source>
         </Image>
    </StackPanel>
</Page>

//File:Window.xaml.cs
using System;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Controls;
using System.Windows.Navigation;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;


namespace ImageElementExample
{
   public partial class CroppedImageExample : Page
   {
      public CroppedImageExample()
      {
      }

      public void PageLoaded(object sender, RoutedEventArgs args)
      {
         Image croppedImage = new Image();
         croppedImage.Width = 200;
         croppedImage.Margin = new Thickness(5);

         CroppedBitmap cb = new CroppedBitmap((BitmapSource)this.Resources["masterImage"],new Int32Rect(30, 20, 105, 50));  
         croppedImage.Source = cb;                 

         Image chainImage = new Image();
         chainImage.Width = 200;

         CroppedBitmap chained = new CroppedBitmap(cb,new Int32Rect(30, 0, (int)cb.Width-30, (int)cb.Height)); 
         chainImage.Source = chained;
      }
   }
}

   
    
     


BitmapImage as Resources


   
  

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="ImageElementExample.FormatConvertedExample"
    Title="FormatConverted Example"
    Loaded="PageLoaded">
   <Page.Resources>
      <BitmapImage x:Key="masterImage" UriSource="c:image.jpg" />
   </Page.Resources>
   <DockPanel>
      <Image Width="200" Source="{StaticResource masterImage}" />
      <Grid Name="convertedGrid" DockPanel.Dock="Top">
         <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="Auto" />
         </Grid.ColumnDefinitions>
         <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
         </Grid.RowDefinitions>
         <Image Width="200" Grid.Column="0" Grid.Row="1">
            <Image.Source>
               <FormatConvertedBitmap Source="{StaticResource masterImage}"  DestinationFormat="Gray4" />
            </Image.Source>
         </Image>
      </Grid>
   </DockPanel>
</Page>
//File:Window.xaml.cs
using System;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Controls;
using System.Windows.Navigation;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace ImageElementExample
{
   public partial class FormatConvertedExample : Page
   {
      public FormatConvertedExample()
      {
      }
      public void PageLoaded(object sender, RoutedEventArgs args)
      {
         Image grayImage = new Image();
         grayImage.Width = 200;
         grayImage.Margin = new Thickness(5);

         FormatConvertedBitmap fcb = new FormatConvertedBitmap((BitmapImage)this.Resources["masterImage"],PixelFormats.Gray4,null,0);
         grayImage.Source = fcb;

         Grid.SetColumn(grayImage, 2);
         Grid.SetRow(grayImage, 1);
         convertedGrid.Children.Add(grayImage);
      }
   }
}

   
    
     


Load Assembly Resources


   
  
<Window x:Class="AssemblyResources.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="AssemblyResources" Height="300" Width="300">
    <StackPanel>      
      <Button Click="cmdPlay_Click" Padding="5">Play</Button>
      <Image Name="img" Source="c:image.jpg"></Image>
      <MediaElement Name="Sound" Source="c:sound.wav" LoadedBehavior="Manual"></MediaElement>
    </StackPanel>
</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;
using System.Reflection;
using System.Resources;
using System.IO;
using System.Globalization;
using System.Windows.Resources;

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

        private void cmdPlay_Click(object sender, RoutedEventArgs e)
        {
            img.Source = new BitmapImage(new Uri("pack://application:,,,/image.jpg"));
            Sound.Stop();
            Sound.Play();
        }
    }
}

   
    
     


Dynamic Resource


   
  

<Window x:Class="Resources.DynamicResource"
    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" x:Name="DynamicBrush" TileMode="Tile"
                ViewportUnits="Absolute" Viewport="0 0 32 32"
                ImageSource="c:image.jpg"></ImageBrush>
  </Window.Resources>
  <StackPanel Margin="5">
    <Button Background="{DynamicResource TileBrush}" >Button</Button>
    <Button Click="cmdChange_Click" >Change the Brush</Button>
  </StackPanel>
</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 Resources
{
    public partial class DynamicResource : System.Windows.Window
    {
        public DynamicResource()
        {
            InitializeComponent();
        }

        private void cmdChange_Click(object sender, RoutedEventArgs e)
        {
            this.Resources["TileBrush"] = new SolidColorBrush(Colors.LightBlue);
            
            ImageBrush brush = (ImageBrush)this.Resources["TileBrush"];
            brush.Viewport = new Rect(0, 0, 5, 5);            
        }
    }
}

   
    
     


DataGridView and Resource


   
  

<Window x:Class="HostingAWinFormsControl.Window1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
  xmlns:local="clr-namespace:HostingAWinFormsControl"
  Title="HostingAWinFormsControl">
  <Grid>
    <Grid.Resources>
      <local:People x:Key="Family">
        <local:Person Name="A" Age="11" />
        <local:Person Name="B" Age="12" />
        <local:Person Name="C" Age="37" />
      </local:People>
    </Grid.Resources>

    <WindowsFormsHost>
      <wf:DataGridView DataSource="{StaticResource Family}" />
    </WindowsFormsHost>
  </Grid>
</Window>
//File:Window.xaml.cs

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Shapes;

namespace HostingAWinFormsControl {
  public class Person {
    string name;
    public string Name {
      get { return name; }
      set { name = value; }
    }

    int age;
    public int Age {
      get { return age; }
      set { age = value; }
    }

    public Person() { }

    public Person(string name, int age) {
      this.name = name;
      this.age = age;
    }
  }

  public class People : System.Collections.Generic.List<Person> { }

  public partial class Window1 : Window {
    public Window1() {
      InitializeComponent();

      //gridView.DataSource = new Person[] {
      //  new Person("A", 11),
      //  new Person("B", 12),
      //  new Person("C", 37)
      //};
    }
  }

}