Click here to Skip to main content
15,885,244 members
Articles / Desktop Programming / WPF

Moving Toward WPF Data Binding One Step at a Time

Rate me:
Please Sign up or sign in to vote.
4.92/5 (110 votes)
19 May 2008CPOL6 min read 350.6K   3.8K   136  
A gradual introduction to the world of WPF data binding.
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;

namespace DatabindingDemo
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();

            // Only one of the following method
            // calls should be uncommented at a time.

            this.ManuallyMoveData();

            //this.BindInCode();

            //this.BindInXaml();
        }

        #region ManuallyMoveData

        Person _person;

        // This method is invoked by the Window's constructor.
        private void ManuallyMoveData()
        {
            _person = new Person
            {
                FirstName = "Josh",
                LastName = "Smith"
            };

            this.firstNameTextBox.Text = _person.FirstName;
            this.lastNameTextBox.Text = _person.LastName;
            this.fullNameTextBlock.Text = _person.FullName;

            this.firstNameTextBox.TextChanged += firstNameTextBox_TextChanged;
            this.lastNameTextBox.TextChanged += lastNameTextBox_TextChanged;
        }

        void lastNameTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            _person.LastName = this.lastNameTextBox.Text;
            this.fullNameTextBlock.Text = _person.FullName;
        }

        void firstNameTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            _person.FirstName = this.firstNameTextBox.Text;
            this.fullNameTextBlock.Text = _person.FullName;
        }

        #endregion // ManuallyMoveData

        #region BindInCode

        private void BindInCode()
        {
            var person = new Person
            {
                FirstName = "Josh",
                LastName = "Smith"
            };

            Binding b = new Binding();
            b.Source = person;
            b.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
            b.Path = new PropertyPath("FirstName");
            this.firstNameTextBox.SetBinding(TextBox.TextProperty, b);

            b = new Binding();
            b.Source = person;
            b.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
            b.Path = new PropertyPath("LastName");
            this.lastNameTextBox.SetBinding(TextBox.TextProperty, b);

            b = new Binding();
            b.Source = person;
            b.Path = new PropertyPath("FullName");
            this.fullNameTextBlock.SetBinding(TextBlock.TextProperty, b);
        }

        #endregion // BindInCode        

        #region BindInXaml

        private void BindInXaml()
        {
            base.DataContext = new Person
            {
                FirstName = "Josh",
                LastName = "Smith"
            };
        }

        #endregion // BindInXaml        
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Software Developer (Senior)
United States United States
Josh creates software, for iOS and Windows.

He works at Black Pixel as a Senior Developer.

Read his iOS Programming for .NET Developers[^] book to learn how to write iPhone and iPad apps by leveraging your existing .NET skills.

Use his Master WPF[^] app on your iPhone to sharpen your WPF skills on the go.

Check out his Advanced MVVM[^] book.

Visit his WPF blog[^] or stop by his iOS blog[^].

See his website Josh Smith Digital[^].

Comments and Discussions