Software & Finance





WPF Binding Example – Student Information Management





 

Here is the complete source code for student information management explaining the concept of WPF Binding.

 

 

 

<Window x:Class="ListBoxDemo.StudentInformationWindow"

    xmlns:mynamespace="clr-namespace:ListBoxDemo"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Title="Student Information Management" Height="370" Width="330">

  

   <Grid Name="MyGrid">

 

      <Grid.Resources>

         <mynamespace:StudentInformation x:Key="CurrentStudent"/>

         <mynamespace:StudentInformation x:Key="AnotherStudent"/>

      </Grid.Resources>

      <Grid.DataContext>

         <Binding Source="{StaticResource CurrentStudent}"/>

     </Grid.DataContext>

     

      <Grid.RowDefinitions>

         <RowDefinition Height="70"></RowDefinition>

         <RowDefinition Height="35"></RowDefinition>

         <RowDefinition Height="*"></RowDefinition>

      </Grid.RowDefinitions>

      

       <StackPanel Orientation="Vertical" Grid.Row="0">

         <TabPanel Margin="0,5,0,0">

            <Label Content="Name:" Width="50"></Label>

            <TextBox Name="gui_studentName" Width="220" Text="{Binding Path=Name, Mode=TwoWay}"></TextBox>

         </TabPanel>

          <TabPanel Margin="0,10,0,0">

            <Label Content="City:" Width="50"></Label>

            <TextBox Name="gui_studentCity" Width="220"  Text="{Binding Path=City, Mode=TwoWay}"></TextBox>

         </TabPanel>

      </StackPanel>

 

      <StackPanel Orientation="Horizontal" Grid.Row="1">

         <Button Name="Add" Content="Add" Click="Add_Click" Width="50" Height="24" Margin="10,0,0,0" HorizontalAlignment="Center"  VerticalAlignment="Center"></Button>

         <Button Name="Edit" Content="Edit" Click="Edit_Click" Width="50" Height="24" Margin="10,0,0,0" HorizontalAlignment="Center"  VerticalAlignment="Center"></Button>

         <Button Name="Update" Content="Update" Click="Update_Click" Width="50" Height="24" Margin="10,0,0,0" HorizontalAlignment="Center"  VerticalAlignment="Center"></Button>

         <Button Name="Delete" Content="Delete" Click="Delete_Click" Width="50" Height="24" Margin="10,0,0,0" HorizontalAlignment="Center"  VerticalAlignment="Center"></Button>

         <Button Name="Event" Content="Event" Click="Event_Click" Width="50" Height="24" Margin="10,0,0,0" HorizontalAlignment="Center"  VerticalAlignment="Center"></Button>

      </StackPanel>

        

      

       <ListBox Grid.Row="2" Name="gui_studentListBox"  Width="290" Height="176" Margin="15,22,0,0" VerticalAlignment="Top"

                 HorizontalAlignment="Left" Visibility="Visible" SelectionMode="Single" ItemsSource="{Binding}"

                     SelectionChanged="gui_studentListBox_SelectionChanged">

         <ListBox.ItemTemplate>

            <DataTemplate>

               <Grid>

                  <Grid.ColumnDefinitions>

                     <ColumnDefinition Width="150"></ColumnDefinition>

                     <ColumnDefinition Width="*"></ColumnDefinition>

                  </Grid.ColumnDefinitions>

 

                  <TextBlock Grid.Column="0" Text="{Binding Name}"  FontSize="12"

                                       Background="Transparent" Height="18" VerticalAlignment="Center">

                  </TextBlock>

                 

                  <TextBlock Grid.Column="1" Text="{Binding City}"  FontSize="12"

                                       Background="Transparent" Height="18" VerticalAlignment="Center">

                  </TextBlock>

 

                 

               </Grid>

            </DataTemplate>

         </ListBox.ItemTemplate>

      </ListBox>

   </Grid>

</Window>

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Data;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Imaging;

using System.Windows.Navigation;

using System.Windows.Shapes;

using System.ComponentModel;

using System.Collections.ObjectModel;

 

namespace ListBoxDemo

{

   /// <summary>

   /// Interaction logic for Window1.xaml

   /// </summary>

   public partial class StudentInformationWindow : Window

   {

 

      public StudentInformationWindow()

      {

         InitializeComponent();

         gui_studentListBox.DataContext = new StudentInformationCollection();

         StudentInformationCollection collection = (StudentInformationCollection)gui_studentListBox.DataContext;

 

         collection.Add(new StudentInformation() { Name = "Kathir", City = "San Francisco" });

         collection.Add(new StudentInformation() { Name = "David", City = "New York" });

         collection.Add(new StudentInformation() { Name = "Xie", City = "Boston" });

         collection.Add(new StudentInformation() { Name = "Linda", City = "Washington" });

         collection.Add(new StudentInformation() { Name = "Jene", City = "New York" });

      }

 

      private void Add_Click(object sender, RoutedEventArgs e)

      {

         StudentInformationCollection collection = (StudentInformationCollection) gui_studentListBox.DataContext;

         StudentInformation target = new StudentInformation();

         StudentInformation source = (StudentInformation)MyGrid.DataContext;

         target.Name = source.Name;

         target.City = source.City;

         collection.Add(target);

      }

 

      private void Edit_Click(object sender, RoutedEventArgs e)

      {

         if (gui_studentListBox.SelectedIndex >= 0)

         {

            StudentInformationCollection collection = (StudentInformationCollection)gui_studentListBox.DataContext;

            StudentInformation target = (StudentInformation)MyGrid.DataContext;

 

            StudentInformation source = collection[gui_studentListBox.SelectedIndex];

            target.Name = source.Name;

            target.City = source.City;

         }

      }

 

      private void Delete_Click(object sender, RoutedEventArgs e)

      {

         if (gui_studentListBox.SelectedItem != null)

         {

            StudentInformationCollection collection = (StudentInformationCollection)gui_studentListBox.DataContext;

            collection.Remove((StudentInformation)gui_studentListBox.SelectedItem);

         }

        

      }

 

      private void Update_Click(object sender, RoutedEventArgs e)

      {

         StudentInformationCollection collection = (StudentInformationCollection)gui_studentListBox.DataContext;

         StudentInformation target = collection[gui_studentListBox.SelectedIndex];

 

         StudentInformation source = (StudentInformation)MyGrid.DataContext;

         target.Name = source.Name;

         target.City = source.City;

      }

 

      private void gui_studentListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)

      {

         if (gui_studentListBox.SelectedIndex >= 0)

         {

            StudentInformationCollection collection = (StudentInformationCollection)gui_studentListBox.DataContext;

            StudentInformation target = (StudentInformation)MyGrid.DataContext;

 

            StudentInformation source = collection[gui_studentListBox.SelectedIndex];

            target.Name = source.Name;

            target.City = source.City;

         }

      }

 

      private void Event_Click(object sender, RoutedEventArgs e)

      {

         for (int i = 0; i < gui_studentListBox.Items.Count; i++)

         {

            StudentInformation item = (StudentInformation)gui_studentListBox.Items[i];

            ListBoxItem lbi = (ListBoxItem)gui_studentListBox.ItemContainerGenerator.ContainerFromItem(item);

            lbi.MouseDoubleClick += new MouseButtonEventHandler(lbi_MouseDoubleClick);

         }

 

      }

 

      void lbi_MouseDoubleClick(object sender, MouseButtonEventArgs e)

      {

         ListBoxItem item = (ListBoxItem) sender;

         StudentInformation student = (StudentInformation) item.Content;

         MessageBox.Show(student.ToString(), "Run time inserted event");

      }  

   }

 

   public class StudentInformationCollection : ObservableCollection<StudentInformation>

   {

   }

 

   public class StudentInformation : INotifyPropertyChanged

   {

      public string _name;

      public string _city;

 

      public event PropertyChangedEventHandler PropertyChanged;

 

      protected void NotifyPropertyChanged(string property)

      {

         if (PropertyChanged != null)

         {

            PropertyChanged(this, new PropertyChangedEventArgs(property));

         }

      }

 

      public string Name

      {

         get

         {

            return _name;

         }

         set

         {

            _name = value; NotifyPropertyChanged("Name");

         }

      }

 

      public string City

      {

         get

         {

            return _city;

         }

         set

         {

            _city = value; NotifyPropertyChanged("City");

         }

      }

 

      public override string  ToString()

      {

            return Name + " " + City;

      }

   }

 

}