Home > WPF > WPF Tutorial – Part 1 : WPF DataBinding

WPF Tutorial – Part 1 : WPF DataBinding

Nowadays everyone running behind WCF and WPF. I have come across many articles related to these topics while googling. I hope you too have read lot of articles on these topics. But here am not going to bore you with definitions and keywords. According to me,learning by example is always a good way of learning new things, that’s why am going to expose the knowledge i have acquired via examples. In this article, will provide some practical examples which reviews some important features of Windows Presentation Foundation. It covers the following features :

  • WPF DataBinding
  • WPF Styles and Control Templates
  • WPF Commands and Custom WPF Commands
  • WPF Dispatcher
  • WPF Resources, Layouts
  • WPF CLR properties and Dependency Properties

These will done in a series of articles, one at a time.

This article is the first in a series, the entire series barely scratches the surface of the immense WPF platform, and it does not dive too deeply into core level on any specific topic. The purpose of this series is to familiarize the reader with the basic WPF programming model.

WPF DataBinding Example: Display a set of data in ListView.

DataBinding.xaml :

<Window x:Class=”DataBinding.DataBinding”
xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation”
xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”
Title=”DataBinding example” Height=”300″ Width=”300″>
<Window.Resources>
<Style x:Key=”TextBlockStyle” TargetType=”{x:Type TextBlock}”>
<Setter Property=”Foreground” Value=”Black” />
</Style>
<Style x:Key=”GridHeaderStyle” TargetType=”{x:Type GridViewColumnHeader}”>
<Setter Property=”Foreground” Value=”#356CAD” />
<Setter Property=”FontSize” Value=”12pt” />
</Style>
</Window.Resources>
<Grid>
<ListView x:Name=”ItemsListView”>
<ListView.View>
<GridView>
<GridViewColumn>
<GridViewColumnHeader Content=”Item Name”  Style=”{StaticResource GridHeaderStyle}” />
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text=”{Binding Name}”  Style=”{StaticResource TextBlockStyle}”></TextBlock>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn>
<GridViewColumnHeader Content=”Item Rate”  Style=”{StaticResource GridHeaderStyle}”/>
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text=”{Binding Rate}”></TextBlock>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</Grid>
</Window>

DataBinding.xaml.cs :

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.Collections.ObjectModel;

namespace DataBinding
{
///
/// Interaction logic for Window1.xaml
///
public partial class DataBinding : Window
{
public DataBinding()
{
InitializeComponent();

ObservableCollection oList = new ObservableCollection();
oList.Add(new Items{ Name=”Ram”, Rate=”23.8″});
oList.Add(new Items { Name = “Mother Board”, Rate = “10303.56″ });
oList.Add(new Items { Name = “SMPS”, Rate = “3421.4″ });
oList.Add(new Items { Name = “KeyBoard”, Rate = “200.0″ });
oList.Add(new Items { Name = “Monitor”, Rate = “3421.4″ });
CollectionViewSource dataSource = new CollectionViewSource { Source=oList };
ItemsListView.ItemsSource = dataSource.View;
}
}
}

item.cs :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DataBinding
{
class Items
{
public string Name { get; set; }

public string Rate { get; set; }
}
}

Download Source : Wpf-DataBinding-Example – 1

  1. No comments yet.
  1. No trackbacks yet.