Find Resource with FindResource


   
  



<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Loaded="Window_Loaded" Title="WPF" Height="230" Width="140">
    <Window.Resources>
        <Style x:Key="labelStyle1">
            <Setter Property="Label.Background" Value="LightYellow" />
            <Setter Property="Label.HorizontalContentAlignment" Value="Center" />
        </Style>
        <Style x:Key="imageStyle1">
            <Setter Property="Image.Source" Value="c:image.png" />
            <Setter Property="Image.Height" Value="140" />
            <Setter Property="Image.Width" Value="96" />
        </Style>
        <Style x:Key="labelStyle2">
            <Setter Property="Label.Background" Value="AliceBlue" />
            <Setter Property="Label.Foreground" Value="DarkBlue" />
        </Style>
        <Style x:Key="imageStyle2">
            <Setter Property="Image.Source" Value="c:image.png" />
            <Setter Property="Image.Height" Value="140" />
            <Setter Property="Image.Width" Value="96" />
        </Style>
    </Window.Resources>
    <StackPanel>
        <Image x:Name="img"/>
        <Label x:Name="lbl" Content="Hello" />
    </StackPanel>
</Window>

//File:Window.xaml.cs

using System;
using System.Windows;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            lbl.Style = (Style)FindResource("labelStyle2");
            img.Style = (Style)FindResource("imageStyle2");
            //lbl.Style = (Style)FindResource("labelStyle1");
            //img.Style = (Style)FindResource("imageStyle1");

        }
    }
}

   
    
     


Create Collection based Resource for data binding


   
  

<Window 
    x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication1"
    Title="WPF" Height="300" Width="180">

    <Window.Resources>
        <local:SortableCountries x:Key="sortableCountries"/>
    </Window.Resources>
    <Grid>
        <StackPanel>
            <ItemsControl ItemsSource="{StaticResource sortableCountries}" />
            <Button Click="SortButton_Click" Content="Sort" Margin="8" />
        </StackPanel>

    </Grid>

</Window>

//File:Window.xaml.cs

using System;
using System.Collections;
using System.Windows;
using System.Windows.Data;
using System.Collections.ObjectModel;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void SortButton_Click(object sender, RoutedEventArgs args)
        {
            SortableCountries sortableCountries = (SortableCountries)(this.Resources["sortableCountries"]);

            ListCollectionView lcv = (ListCollectionView)CollectionViewSource.GetDefaultView(sortableCountries);

            lcv.CustomSort = new SortCountries();
        }
    }

    public class SortCountries : IComparer
    {
        public int Compare(object x, object y)
        {
            string stringX = x.ToString();
            string stringY = y.ToString();
            
            return stringX.CompareTo(stringY);

        }
    }

    public class SortableCountries : ObservableCollection<string>
    {
        public SortableCountries()
        {
            this.Add("C");
            this.Add("B");
            this.Add("A");
        }
    }
}

   
    
     


Wrap collection based resource in a CollectionViewSource


   
  

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication1"
    Title="WPF" Height="124" Width="124">
    <Window.Resources>
        <local:Countries x:Key="countries"/>
        <CollectionViewSource x:Key="cvs" Source="{Binding Source={StaticResource countries}}"
            Filter="CollectionViewSource_EuropeFilter" />
    </Window.Resources>
    <Grid>

        <ItemsControl ItemsSource="{Binding Source={StaticResource cvs}}"
            DisplayMemberPath="Name"/>
    </Grid>

</Window>
//File:Window.xaml.cs
using System.Windows;
using System.Windows.Data;
using System.Collections.ObjectModel;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }
        private void CollectionViewSource_EuropeFilter(object sender, FilterEventArgs e)
        {
            Country country = e.Item as Country;
            e.Accepted = (country.Continent == Continent.Europe);
        }
    }

    public class Country
    {
        private string name;
        private Continent continent;

        public string Name
        {
            get{ return name;}
            set{name = value;}
        }

        public Continent Continent
        {
            get{return continent;}
            set{continent = value;}
        }

        public Country(string name, Continent continent)
        {
            this.name = name;
            this.continent = continent;
        }
    }

    public enum Continent
    {
        Europe,
        NorthAmerica,
    }

    public class Countries : Collection<Country>
    {
        public Countries()
        {
            this.Add(new Country("Great Britan", Continent.Europe));
            this.Add(new Country("USA", Continent.NorthAmerica));
            this.Add(new Country("Canada", Continent.NorthAmerica));
        }
    }
}

   
    
     


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>

   
    
    
    
    
    
    
     


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>

   
    
    
    
    
    
    
     


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>

   
    
    
    
    
    
    
     


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>