Posted by: Zeeshan Amjad | February 16, 2010

Data Binding with tool bar


Today we are going to discuss one very small program to perform data binding with tool bar. We have already saw the example of data binding with Tab control here.  Out this program is not very much different from tab control version.

We create one tool bar in XAML and define its data template. We simply define the button control in its data template. Here is complete C# code of the program.

  1: <Window x:Class="WpfToolBar.Window1"
  2:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4:     Title="Toolbar" Height="300" Width="400">
  5:     <DockPanel>
  6:         <ToolBarTray DockPanel.Dock="Top" Background="White">
  7:             <ToolBar Name="toolbar" Band="1" BandIndex="1">
  8:                 <ToolBar.ItemTemplate>
  9:                     <DataTemplate>
 10:                         <Grid>
 11:                             <Button Margin="2" Content="{Binding}"/>
 12:                         </Grid>
 13:                     </DataTemplate>
 14:                 </ToolBar.ItemTemplate>
 15:             </ToolBar>
 16:         </ToolBarTray>
 17:         <Grid>
 18: 
 19:         </Grid>
 20:     </DockPanel>
 21: </Window>
 22: 

In next step we create a list of string in C# and set it to the items source property of tool bare. 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 WpfToolBar
 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> list = new List<string>();
 27:             list.Add("Maryland");
 28:             list.Add("Virginia");
 29:             list.Add("Washington");
 30:             list.Add("Californi");
 31: 
 32:             toolbar.ItemsSource = list;
 33:         }
 34:     }
 35: }
 36: 

That’s all we have to do to perform data binding with tool bar. And it is very much similar to the tab control program. Here is the output of this program.

ToolBarBindingOutput

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: