Posts
17
Comments
3945
Trackbacks
21
A little bit of WPF DataBinding...

Let's assume you wish to use DataBinding to manage a set of Person class instances.

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.ObjectModel;

 public class Person
 {
  public Person (string name, int age)
  {
   this.Name = name;
   this.Age = age;
  }

  private int mAge;
  public int Age
  {
   get { return mAge; }
   set { mAge = value; }
  }

  private string mName;
  public string Name
  {
   get { return mName; }
   set { mName = value; }
  }

  public override string ToString ()
  {
   return this.Name + " : " + this.Age.ToString();
  }

Now we create a Persons class inheriting from generic ObservableCollection

public class Persons:ObservableCollection<Person>
 {
  public Persons ()
  {
   base.Add(new Person("Corrado",22));
   base.Add(new Person("Giulia",1));
   base.Add(new Person("Stefania",10));
  }
 }


We now use ObjectDataSource to create Persons instance and have it available as DataContext inside xaml.

<?Mapping XmlNamespace="tds" ClrNamespace="Demo" ?>
<Window x:Class="Demo.Window1"
    xmlns="
http://schemas.microsoft.com/winfx/avalon/2005"
    xmlns:x="
http://schemas.microsoft.com/winfx/xaml/2005"
    xmlns:tds="tds"
    Title="Demo"
 >
 <Window.Resources>
  <ObjectDataProvider x:Key="Persons" ObjectType="{x:Type tds:Persons}"  />
  
  <DataTemplate DataType="{x:Type tds:Person}">
   <StackPanel Orientation="Horizontal">
    <TextBlock Foreground="Red" Text="{Binding Name}" FontSize="14" />
    <TextBlock Foreground="Blue" Text="{Binding Age}" FontSize="14"/>
   </StackPanel>
  </DataTemplate>  

 </Window.Resources>

<Grid>
   <ListBox ItemsSource="{Binding Source={StaticResource Persons}}" />    
</Grid>
</Window>
    
       
   
Red lines are namespace to Xml mapping instructions (syntax is probably going to be modified on next CTP)
Magenta line is the ObjectDataProvider definition that allows us to use Person list as datasource on the listbox.
Green lines indicates that both ObjectDataProvider and DataTemplate objects are part of Windows Resources.
DataTemplate (in blue) describes how the UI should display the Person instance (otherwise the ToString() method it will be invoked) in this case I decided to use a StackPanel containing two textblocks both binding to Person properties.
Data are databound to listbox by ItemSource attribute (in orange)
Datatemplates are required because on WPF ItemsControls (like ListBox) are not limited to strings but can contain virtually everything.

Just a simpe demo, but as you know, we all need a starting point... :-)

posted on venerdì 10 febbraio 2006 01:34 Print
Comments
Gravatar
# re: A little bit of WPF DataBinding...
Michele Bersani
10/02/2006 06:55
Corrado
there must a problem with the formatting.
Most of the code is not visible.
Ciao
Gravatar
# re: A little bit of WPF DataBinding...
Corrado Cavalli
10/02/2006 10:49
Thanks Michele
Gravatar
# re: A little bit of WPF DataBinding...
vijay
23/01/2007 13:46
I have copied ur code tried . It runs without any arror but not able to see the results.
Gravatar
# re: A little bit of WPF DataBinding...
siri
31/01/2007 15:39
i have tried to convert ur code to vb.net but iam not able to get this in vb.net. i got so many errors in converting this code
Gravatar
# re: A little bit of WPF DataBinding...
siri
31/01/2007 16:20
i have tried to execute the same code but iam getting the error like........
The type reference cannot find a public type named 'Persons'. Line 9 Position 39.

can anybody help me

Thankx
Gravatar
# re: A little bit of WPF DataBinding...
Vietnam tours
02/05/2013 14:35
A post good and interneting.
Gravatar
# wholesale sexylingerie
janebetty
06/07/2013 08:47
Dizzy at the sight of the code
Gravatar
# re: A little bit of WPF DataBinding...
stylishplus
12/08/2013 14:20

A dress with lady like floral prints is also a must. A knee length or tea length floral dress is best for garden parties, backyard barbecues, evening events and summer weddings. Women with a bold personality can opt for bright, big prints and the shy ones might prefer cute and small ditsy floral print.
Gravatar
# "Kigurumi" comes from a combination of two Japanese words: kiru <a href="http://www.hotonesie.com/">hotonesie.com</a> "to wear"
Home
07/11/2013 11:38
"Kigurumi" comes from a combination of two Japanese words: kiru hotonesie.com "to wear" and nuigurumi "stuffed toy". Traditionally, it referred specifically to the performers wearing the costume, but the word has since grown to include the costumes themselves. In Japan, kigurumi costumes hotonesie.com are worn as a way to break the conventions of traditional dress… or just as comfy pajamas! Our kigurumis come in two different kinds of fabric – fleece and pile. The fleece fabric is slightly heavier, making these kigurumi a good choice for pajamas or outdoor wear. kigurumi The thinner pile fabric is good for warmer environments, such as indoor parties or hot climates.
Gravatar
# Cosplay is a <a href="http://www.hotonesie.com/">Japanese</a> hobby of dressing up like Anime Manga and Video Game characters.
Home
07/11/2013 11:42
Cosplay is a Japanese hobby of dressing up like Anime Manga and Video Game characters. Although recently it has included many other media forms, such as movie and literature characters. Cosplay has become a vastly spreading hobby here in the states. If you have ever been to a convention there is no doubt you have seen at least a hundred Ninjas running around. For those of you who have never attended a convention,http://www.hotonesie.com/ you are in for a treat. We welcome and encourage all family friendly cosplay here at AAC. However please read the weapons policy before attending, we would hate to have to take away a Home prop you have been working on for a half year.Welcome to Another Anime Conventions Cosplay and Masquerade page. Below are our numerous events planned this year and general information about hotonesie.com Cosplay.
Gravatar
# Other ItemsSource in WPF | Jptab
Pingback/TrackBack
14/11/2013 21:48
Other ItemsSource in WPF | Jptab
Gravatar
# re: A little bit of WPF DataBinding...
Agen Ibcbet
23/01/2014 09:57
I'm now not sure the place you are getting your information, but good topic.
Gravatar
# I was thinking my life was done.
sbobet
13/02/2014 05:52
I wish to show thanks to you just for bailing me out of this particular trouble.As a result of checking through the the net and meeting techniques that were not productive, I was thinking my life was done.
Gravatar
# latest news headlines
headlines today
26/05/2014 11:26
In another major development, Pakistan has so far not dropped any hint that Mr. Sharif will meet the Kashmiri leaders which is also a departure from the established tradition.
Gravatar
# re: A little bit of WPF DataBinding...
vietnam holidays
03/06/2014 14:48
This code analysis somewhat difficult.
Gravatar
# re: A little bit of WPF DataBinding...
distributor jaket kulit
27/02/2015 15:58
the info in this blog gives a good note and make visitors feel at home visiting the blog this one

Gravatar
# re: A little bit of WPF DataBinding...
jaket kulit asli
05/05/2015 09:27
new things and new experiences visiting this blog I really like the articles and posts written on this blog thanks admin
Gravatar
# re: A little bit of WPF DataBinding...
Furnishings and Supplies
08/07/2015 13:02
Rattan garden furniture might perhaps be confused using a number of other material for example wicker. This Rattan is really a relative including what's referred to as some kind of tropical hand sapling.
Gravatar
# re: A little bit of WPF DataBinding...
http://jaketkulitgarut.id
05/08/2015 21:51
be a reflection and a lot of knowledge to visit this blog and interesting to read thanks admin
Gravatar
# re: A little bit of WPF DataBinding...
anditoko
12/09/2015 11:16
add insight into a broad and obtain useful information thanks admin.http://jualjaketkulit.co.id visit back my website
Gravatar
# re: A little bit of WPF DataBinding...
aza
08/10/2015 22:15
we can no longer deny each blog provide useful news and useful for all who visit. jaket kulit pria visit back LOL
Gravatar
# re: A little bit of WPF DataBinding...
send free sms
30/04/2017 12:25
Send online Friendship (Dosti) SMS in Egnlish & Urdu in Pakistan and India. for mobile and whatsapp.
Gravatar
# re: A little bit of WPF DataBinding...
five nights at freddy’s
16/08/2017 14:31
Very helpful advice in this particular post! It’s the little changes that make the largest changes. Thanks for sharing!
Gravatar
# SEM agency
SEM agency
25/11/2017 18:58
Hey there, I think your site might be having browser compatibility issues. When I look at your blog site in Chrome, it looks fine but when opening in Internet Explorer, it has some overlapping. I just wanted to give you a quick heads up! Other then that, great blog!
Gravatar
# limo hire melbourne
limo hire melbourne
09/12/2017 18:47
Along with every little thing that seems to be building throughout this area, many of your perspectives are somewhat exciting
Comments have been closed on this topic.
News

This is my personal blog.
These postings are provided "AS IS" with no warranties, and confer no rights.

Disciple