Posted by: Zeeshan Amjad | November 26, 2009

Use Visual Brush to fill the shape with Controls


We have already discuss the usage of Visual Brush and see how we can use it to make a text reflection. This time we are going to make a visual brush to fill the geometrical shape with control. We will do a binding of Visual Brush with UIElement, just like we use to do data binding with other UIElement.

  1: <Ellipse Grid.Row="1" Margin="5">
  2: 	<Ellipse.Fill>
  3: 		<VisualBrush Visual="{Binding ElementName=list}"/>
  4: 	</Ellipse.Fill>
  5: </Ellipse>
  6: 

Here is a class diagram of Brush class.

Brush

Here is a complete program to fill the ellipse with a lix box usingVisual Brush. 

  1: <Window x:Class="VisualBrush.Window1"
  2:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4:     Title="Visual Brush" Height="400" Width="400">
  5:     <Grid>
  6:         <Grid.RowDefinitions>
  7:             <RowDefinition/>
  8:             <RowDefinition/>
  9:         </Grid.RowDefinitions>
 10:         
 11:         <ListBox Grid.Row="0" Name="list" Margin="5" Background="AliceBlue" >
 12:             <ListBox.ItemTemplate>
 13:                 <DataTemplate>
 14:                     <TextBlock FontSize="14" Foreground="Navy" Text="{Binding}"/>
 15:                 </DataTemplate>
 16:             </ListBox.ItemTemplate>    
 17:         </ListBox>
 18:         <Ellipse Grid.Row="1" Margin="5">
 19:             <Ellipse.Fill>
 20:                 <VisualBrush Visual="{Binding ElementName=list}"/>
 21:             </Ellipse.Fill>
 22:         </Ellipse>
 23:     </Grid>
 24: </Window>
 25: 

Here is C# code of this 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 VisualBrush
 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:             List<String> stateList = new List<string>();
 27: 
 28:             stateList.Add("Maryland");
 29:             stateList.Add("California");
 30:             stateList.Add("Virginia");
 31:             stateList.Add("Washington");
 32:             stateList.Add("New York");
 33:             stateList.Add("Nevada");
 34:             stateList.Add("Florida");
 35:             stateList.Add("Arizona");
 36: 
 37:             list.ItemsSource = stateList;
 38:         }
 39:     }
 40: }
 41: 

This is the output of this program.

VisualBrushOutput

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: