Posted by: Zeeshan Amjad | January 11, 2010

Get Default value in Data Binding


Sometimes we need default value when data binding is failed. There might be few cases when it is required, such as the path variable is not correct, converter is not returning the property value etc. In this case we can use the Fallback property of Binding class to use the default value. Here is a syntax to use the default value in Data binding.

  1: <TextBox Name="txtState" Grid.Column="1" Grid.Row="0" VerticalAlignment="Center"
  2: 	 Text="{Binding Path=DummyProperty, FallbackValue=Enter State Name Here}"/>
  3: 

Here we explicitly select the dummy property set with path property. This text box will display the default value i.e. display “Enter State Name Here”.

Let’s make a small program to use this functionality. In this simple program we display the name of few states in list box and user is suppose to enter the name of any state in text box. If that name is present in the list box then it will display the message box “You entered “ with the state name. However if the state name is not present in the list box then it will display “This state is not present in list box.”.

Here is complete XAML code of this program.

  1: <Window x:Class="WpfDataBinding.Window1"
  2:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4:     Title="Default value in Binding" Height="300" Width="300">
  5:     <Grid>
  6:         <Grid.ColumnDefinitions>
  7:             <ColumnDefinition/>
  8:             <ColumnDefinition/>
  9:         </Grid.ColumnDefinitions>
 10:         <Grid.RowDefinitions>
 11:             <RowDefinition/>
 12:             <RowDefinition Height="4*"/>
 13:             <RowDefinition/>
 14:         </Grid.RowDefinitions>
 15:         <TextBlock Grid.Column="0" Grid.Row="0" VerticalAlignment="Center" 
 16:                    Text="Enter State Name"/>
 17:         <TextBox Name="txtState" Grid.Column="1" Grid.Row="0" VerticalAlignment="Center"
 18:                  Text="{Binding Path=DummyProperty, FallbackValue=Enter State Name Here}"/>
 19:         <ListBox Name="list" Margin="5" Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="2">
 20:         </ListBox>
 21:         <Button Grid.Row="2" Grid.ColumnSpan="2" 
 22:                 Click="Button_Click" Margin="101.5,7,101.5,1">Find Capital</Button>
 23:     </Grid>
 24: </Window>
 25: 

C# code of this program is very simple. It just populate the list box and in the button handler check the name of the state user entered in text box. Here is complete 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 WpfDataBinding
 16: {
 17:     /// <summary>
 18:     /// Interaction logic for Window1.xaml
 19:     /// </summary>
 20:     public partial class Window1 : Window
 21:     {
 22:         private List<String> allStates = new List<string>();
 23:         public Window1()
 24:         {
 25:             InitializeComponent();
 26: 
 27:             allStates.Add("Maryland");
 28:             allStates.Add("Virginia");
 29:             allStates.Add("Pennsylvania");
 30:             allStates.Add("Delaware");
 31:             allStates.Add("West Virginia");
 32:             allStates.Add("California");
 33:             allStates.Add("Arizona");
 34:             allStates.Add("Nevada");
 35:             allStates.Add("Washington");
 36: 
 37:             list.ItemsSource = allStates;
 38:         }
 39: 
 40:         private void Button_Click(object sender, RoutedEventArgs e)
 41:         {
 42:             foreach (String str in allStates)
 43:             {
 44:                 if (str == txtState.Text)
 45:                 {
 46:                     MessageBox.Show("You entered " + txtState.Text);
 47:                     return;
 48:                 }
 49:             }
 50: 
 51:             MessageBox.Show("This state is not present in list box.");
 52:         }
 53:     }
 54: }
 55: 

This is the output of this program.

DefaultValueInBinding

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: