Posted by: Zeeshan Amjad | July 19, 2009

Defining Logical Resources in C#


WPF introduce one new type of resource called logical resource. This is WPF specific concept. This type of resources can be defined in XAML as well as in any programming language such as C#, VB.Net or VC++.Net. Here is one simple example to demonstrate this concept. If we are going to add three buttons in stack panel its code is something like this.

  1: <Window x:Class="xamlSample.Window1"
  2:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4:     Title="Resource Sample" Height="300" Width="300">
  5:         
  6:     <StackPanel>
  7:         <Button Name="btnButton1">
  8:             Button 1
  9:         </Button>
 10:         <Button Name="btnButton2">
 11:             Button 2
 12:         </Button>
 13:         <Button Name="btnButton3">
 14:             Button 3
 15:         </Button>
 16:     </StackPanel>
 17: </Window>
 18: 

Here is the output of this XAML code.

wpfResource01

Let’s set its value of its margin and background. Here is the XAML to define this.

  1: <Window x:Class="xamlSample.Window1"
  2:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4:     Title="Resource Sample" Height="300" Width="300">
  5:     
  6:     <Window.Resources>
  7:         <Style TargetType="Button">
  8:             <Setter Property="Margin" Value="5, 5, 5, 5"/>
  9:             <Setter Property="Background" Value="Wheat"/>
 10:         </Style>
 11:     </Window.Resources>
 12:     
 13:     <StackPanel>
 14:         <Button Name="btnButton1">
 15:             Button 1
 16:         </Button>
 17:         <Button Name="btnButton2">
 18:             Button 2
 19:         </Button>
 20:         <Button Name="btnButton3">
 21:             Button 3
 22:         </Button>
 23:     </StackPanel>
 24: </Window>
 25: 

Here is the output of this XML code.

wpfResource02

 

If we want to do the same thing then we will have to create an object of ResourceDictionary. This is one more WPF class that is not inherited from DispatcherObject and internally uses Hashtable class. Here is a C# code to get the same result using the logical resource.

  1: using System;
  2: using System.Windows;
  3: using System.Windows.Media;
  4: using System.Windows.Controls;
  5: 
  6: public class MyWindow : Window
  7: {
  8:     private ResourceDictionary rd;
  9: 
 10:     public MyWindow()
 11:     {
 12:         Title = "Resource Sample";
 13:         Width = 300;
 14:         Height = 300;
 15: 
 16:         StackPanel wp = new StackPanel();
 17: 
 18:         Button btnButton1 = new Button();
 19:         btnButton1.Content = "Button 1";
 20: 
 21:         Button btnButton2 = new Button();
 22:         btnButton2.Content = "Button 2";
 23: 
 24:         Button btnButton3 = new Button();
 25:         btnButton3.Content = "Button 3";
 26: 
 27:         wp.Children.Add(btnButton1);
 28:         wp.Children.Add(btnButton2);
 29:         wp.Children.Add(btnButton3);
 30: 
 31:         Content = wp;
 32: 
 33:         rd = new ResourceDictionary();
 34: 
 35:         rd.Add("Margin", new Thickness(5, 5, 5, 5));
 36:         rd.Add("Background", Brushes.Wheat);
 37: 
 38:         this.Resources = rd;
 39: 
 40:         btnButton1.Margin = (Thickness)FindResource("Margin");
 41:         btnButton1.Background = (Brush)FindResource("Background");
 42: 
 43:         btnButton2.Margin = (Thickness)FindResource("Margin");
 44:         btnButton2.Background = (Brush)FindResource("Background");
 45: 
 46:         btnButton3.Margin = (Thickness)FindResource("Margin");
 47:         btnButton3.Background = (Brush)FindResource("Background");
 48:     }
 49: }
 50: 
 51: public class MyApplication : Application
 52: {
 53: }
 54: 
 55: public class wpf
 56: {
 57:     [STAThread]
 58:     public static void Main()
 59:     {
 60:         MyWindow win = new MyWindow();
 61: 
 62:         MyApplication app = new MyApplication();
 63:         app.Run(win);
 64:     }
 65: }
 66: 
 67: 
 68: 
Advertisements

Responses

  1. […] Style conditionally We have studied how can we apply style. Now we are going to see how can we apply style conditionally. In other words apply different style […]

  2. […] different framework elements in Style We saw how to apply style in our program here. We also see how to use the style based on existing style here. But in all of the examples, we make […]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: