Wrap collection based resource in a CollectionViewSource

image_pdfimage_print


   
  

<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

image_pdfimage_print


   
       

<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

image_pdfimage_print
   
       

<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

image_pdfimage_print


   
       

<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

image_pdfimage_print


   
       
<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>

   
    
    
    
    
    
    
     


Relative pack URI referring to external component for a Resource

image_pdfimage_print


   
             
<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

image_pdfimage_print


   
       

<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>