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)
      //};
    }
  }

}

   
    
     


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);            
        }
    }
}

   
    
     


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();
        }
    }
}

   
    
     


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");
        }
    }
}

   
    
     


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);
            }
        }

    }
}

   
    
     


Retrieving assembly manifest resources

   
  


<Window x:Class="BinaryResources.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="BinaryResources" Height="300" Width="300">
    <Grid>
      <Image Source="c:image.jpg" />
    </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;
using System.IO;
using System.Reflection;
using System.Diagnostics;
using System.Threading;
using System.Resources;
using System.Collections;
using System.Windows.Resources;

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

            Assembly asm = Assembly.GetExecutingAssembly();
            Stream s = asm.GetManifestResourceStream("BinaryResources.MyStream.txt");

            StreamReader r = new StreamReader(s);
            Debug.WriteLine(r.ReadToEnd());
        }
    }
}

   
    
     


Get Resource Names from Assembly


   
  
<Window x:Class="BinaryResources.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="BinaryResources" Height="300" Width="300">
    <Grid>
      <Image Source="c:image.jpg" />
    </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;
using System.IO;
using System.Reflection;
using System.Diagnostics;
using System.Threading;
using System.Resources;
using System.Collections;
using System.Windows.Resources;

namespace BinaryResources
{
    public partial class Window1 : System.Windows.Window
    {
        public Window1()
        {
            InitializeComponent();
            foreach (string streamName in GetResourceNames(typeof(Window1).Assembly, Thread.CurrentThread.CurrentUICulture))
            {
                Debug.WriteLine(streamName);
            }
        }
        static List<string> GetResourceNames(Assembly asm,System.Globalization.CultureInfo culture)
        {

            string resourceName = asm.GetName().Name + ".g";
            ResourceManager rm = new ResourceManager(resourceName, asm);
            ResourceSet resourceSet = rm.GetResourceSet(culture, true, true);
            List<string> resources = new List<string>();
            foreach (DictionaryEntry resource in resourceSet)
            {

                resources.Add((string) resource.Key);
            }
            rm.ReleaseAllResources();
            return resources;
        }


    }
}