Posted by: Zeeshan Amjad | December 19, 2009

Ignore the logical resource


We already studied how to define the logical resource. Here is one simple program to define logical resource and apply it on all button. Here is XAML code of this.

  1: <Window x:Class="WpfLogicalResource.Window1"
  2:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4:     Title="Logical Resource" Height="300" Width="300">
  5:     <Window.Resources>
  6:         <Style TargetType="Button">
  7:             <Setter Property="Foreground" Value="Blue"/>
  8:             <Setter Property="Margin" Value="5"/>
  9:             <Setter Property="Background" Value="Wheat"/>
 10:             <Setter Property="FontSize" Value="16"/>
 11:         </Style>
 12:     </Window.Resources>
 13:     
 14:     <StackPanel>
 15:         <Button Name="btn1">Button 1</Button>
 16:         <Button Name="btn2">Button 2</Button>
 17:         <Button Name="btn3">Button 3</Button>
 18:         <Button Name="btn4">Button 4</Button>
 19:     </StackPanel>
 20: </Window>
 21: 

This would be the output of this program.

Logical_Resource_01

But if we want to exclude to apply any control to use this logical resource then we can use one already define markup extension i.e. null markup extension. Here is a class diagram of all mark up extension classes define in WPF.

MarkupExtension 

Here is the usage of null markup extension.

  1: <Window x:Class="WpfLogicalResource.Window1"
  2:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4:     Title="Logical Resource" Height="300" Width="300">
  5:     <Window.Resources>
  6:         <Style TargetType="Button">
  7:             <Setter Property="Foreground" Value="Blue"/>
  8:             <Setter Property="Margin" Value="5"/>
  9:             <Setter Property="Background" Value="Wheat"/>
 10:             <Setter Property="FontSize" Value="16"/>
 11:         </Style>
 12:     </Window.Resources>
 13:     
 14:     <StackPanel>
 15:         <Button Name="btn1">Button 1</Button>
 16:         <Button Style="{x:Null}" Name="btn2">Button 2</Button>
 17:         <Button Style="{x:Null}" Name="btn3">Button 3</Button>
 18:         <Button Name="btn4">Button 4</Button>
 19:     </StackPanel>
 20: </Window>
 21: 

Here are are excluding template style from second and third button. Here is the output of this program.

Logical_Resource_02

Advertisements

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: