Click here to Skip to main content
13,249,463 members (32,232 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


1 bookmarked
Posted 6 Jan 2011

Different ways to bind a data in Codebind from XAML

, 7 Jan 2011
Rate this:
Please Sign up or sign in to vote.
Databinding Concept in WPF
I see many questions in "Question & Answers" section regarding the data binding issue in WPF. And when I see the question, they miss a very simple thing.

This is a tip to help them out. And basically for beginners who are trying to learn the data binding concepts.

The sample, I provide here is:
- WPF Application
- C# Language

Create a WPF Application from your Visual Studio. Place a ListBox.


<Window x:Class="WpfApplication2.Window1"
        Title="Window1" Height="300" Width="300"
        <ListBox  />

Add a simple Property in the codebehind of Window1 as CollectionOfString which is nothing but List<string>. Add few items into the CollectionOfString for our DataBinding example.


using System.Collections.Generic;
using System.Windows;
namespace WpfApplication2
    public partial class Window1 : Window
        public Window1()
            this.CollectionOfString = new List<string>();
            this.CollectionOfString.Add("Custard Apple");
        public List<string> CollectionOfString

Now go to the designer/XAML window. You simply set binding of the CollectionOfString in two ways like below:

Way 1:

<ListBox ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type loc:Window1}}, Path=CollectionOfString}" />

Here "loc" is the namespace of Window1 class.

Way 2:

<ListBox ItemsSource="{Binding ElementName=windowObject, Path=CollectionOfString}"/>

Here "windowObject" is the name of the Window.

Let me know if you have any questions.

I hope it is helpful.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Venkatesh Mookkan
Team Leader
Australia Australia
He is from a small village in Tirunelveli, TamilNadu (India). He is fun loving guy. He like to travel a lot. Bikes/Cars/Mobiles are his fantasies.

He started his career as a BPO Data Entry Assistant and got chance to work as an ASP.NET Programmer in the same company after a year. And he move to the next company in 2006 as Software Engineer.

He worked as a Team Leader in a private software development company @Chennai, India for 8+years.

Now working as a Consultant @Sydney, Australia.

You may also be interested in...


Comments and Discussions

GeneralWhat about adding a note on how to do it when the Collection... Pin
Michael900017-Oct-11 5:08
memberMichael900017-Oct-11 5:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171114.1 | Last Updated 7 Jan 2011
Article Copyright 2011 by Venkatesh Mookkan
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid