Posted by: Zeeshan Amjad | March 1, 2010

Null Brush and Transparent Brush of Border


 

When defining the background color of border, then we can define two different types of Brush other than predefined color brushes. The first one is transparent and second one is null brush. Visual both seems exactly the same. But there is one main different. When the brush of border brush for background color is transparent, it will still take the mouse input when use click inside the border. On the other hand when border brush for background color is null, then border can’t take any input.

Let’s take a look at the simple program to explore this. Here is complete XAML code of the program.

  1: <Window x:Class="WpfBrush.Window1"
  2:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4:     Title="Null and Transparent Brush" Height="300" Width="300">
  5:     <Grid>
  6:         <Grid.Background>
  7:             <LinearGradientBrush>
  8:                 <GradientStop Offset="0" Color="DarkKhaki"/>
  9:                 <GradientStop Offset="0.5" Color="LightYellow"/>
 10:                 <GradientStop Offset="1" Color="DarkKhaki"/>
 11:             </LinearGradientBrush>
 12:         </Grid.Background>
 13:         <Grid.ColumnDefinitions>
 14:             <ColumnDefinition/>
 15:             <ColumnDefinition/>
 16:         </Grid.ColumnDefinitions>
 17:         <Border Name="border1" Margin="10" Grid.Column="0" BorderBrush="Blue" CornerRadius="10"
 18:                 BorderThickness="5" MouseDown="border1_MouseDown">
 19:         </Border>
 20:         <Border Name="border2" Margin="10" Grid.Column="1" BorderBrush="Red" CornerRadius="10"
 21:                 BorderThickness="5" MouseDown="border2_MouseDown">
 22:         </Border>
 23:     </Grid>
 24: </Window>
 25: 

Now we set the brush of one border transparent and other is null in C# code. Here is complete C# code of the program.

  1: using System;
  2: using System.Collections.Generic;
  3: using System.Linq;
  4: using System.Text;
  5: using System.Windows;
  6: using System.Windows.Controls;
  7: using System.Windows.Data;
  8: using System.Windows.Documents;
  9: using System.Windows.Input;
 10: using System.Windows.Media;
 11: using System.Windows.Media.Imaging;
 12: using System.Windows.Navigation;
 13: using System.Windows.Shapes;
 14: 
 15: namespace WpfBrush
 16: {
 17:     /// <summary>
 18:     /// Interaction logic for Window1.xaml
 19:     /// </summary>
 20:     public partial class Window1 : Window
 21:     {
 22:         public Window1()
 23:         {
 24:             InitializeComponent();
 25: 
 26:             border1.Background = Brushes.Transparent;
 27:             border2.Background = null;
 28:         }
 29: 
 30:         private void border1_MouseDown(object sender, MouseButtonEventArgs e)
 31:         {
 32:             MessageBox.Show("Click inside Border 1", "Brush");
 33:         }
 34: 
 35:         private void border2_MouseDown(object sender, MouseButtonEventArgs e)
 36:         {
 37:             MessageBox.Show("Click inside Border 2", "Brush");
 38:         }
 39:     }
 40: }
 41: 

We define two handler when mouse down. One handler is for each border control that display one message box when user click on it. Here is the output of the program.

BorderBrushOutput

If we click on blue border, then we will see one message box, because its background brush is set to transparent. However if we click on red border then our message handler is not going to invoke. And this is a main difference between transparent and null brush of border control.

Advertisements

Responses

  1. A great bit of WPF wizardry to keep in mind. Very nice. Thanks!


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: