[dba-VB] SCRUM/WPF Data Binding

Salakhetdinov Shamil mcp2004 at mail.ru
Mon Jun 1 17:17:49 CDT 2009


Hi All,

I planned here is the first sample of WPF data bidining:

http://shamils-4.hosting.parking.ru/xaml/WpfBrowserApplication2.xbap

As you can find it promise to be rather time consuming to implement binding to ADO.NET datasets, which isn't supported natively in .NET 3.5. The following sample just implements bindind to an object instance.

Robert, you have experience with VS2010 - does it have advanced data binding controls as e.g. WinForms' BidingSource and BindingNavigator?

Thank you.

--
Shamil

P.S. Here is the code of the referenced above sample page:

Shippers.xaml
=============

<Page
        x:Class="WpfBrowserApplication1.ShippersPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Shippers" Height="180" Width="260">
    <Grid>

        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="74"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="30"/>
            <RowDefinition Height="30"/>
            <RowDefinition Height="30"/>
        </Grid.RowDefinitions>
        <TextBlock
            Margin="4"
            Text="Id"
            VerticalAlignment= "Center"/>
        <TextBox
            Text="{Binding Path=Id, Mode=TwoWay}"
            Margin="4" Grid.Column="1"/>
        <TextBlock
            Margin="4"
            Text="Name"
            Grid.Row="1"
            VerticalAlignment="Center"/>
        <TextBox
            Margin="4"
            Text="{Binding Path=Name, Mode=TwoWay}"
            Grid.Column="1" Grid.Row="1"/>
        <TextBlock
            Margin="4"
            Text="Phone"
            Grid.Row="2"
            VerticalAlignment="Center"/>
        <TextBox
            Margin="4"
            Text="{Binding Path=Phone, Mode=TwoWay}"
            Grid.Column="1"
            Grid.Row="2"/>
    </Grid>
</Page>


Shippers.xaml.cs
================

using System.Windows.Controls;
using System.ComponentModel;

namespace WpfBrowserApplication1
{
    public partial class ShippersPage : Page
    {
        public ShippersPage()
        {
            InitializeComponent();
            // Set the DataContext 
            // to a Shipper object
            this.DataContext =
            new Shipper()
            {
                Id = 1,
                Name = "Speedy Express",
                Phone = "(503) 555-9831"
            };
        }
    }

    public class Shipper : INotifyPropertyChanged
    {
        private int _id;

        public int Id
        {
            get { return _id; }
            set
            {
                if (_id != value)
                {
                    _id = value;
                    OnPropertyChanged("Id");
                }
            }
        }
        private string _name;

        public string Name
        {
            get { return _name; }
            set
            {
                _name = value;
                if (_name != value)
                {
                    _phone = value;
                    OnPropertyChanged("Name");
                }
            }
        }
        private string _phone;

        public string Phone
        {
            get { return _phone; }
            set
            {
                _phone = value;
                if (_phone != value)
                {
                    _phone = value;
                    OnPropertyChanged("Phone");
                }
            }
        }

        #region INotifyPropertyChanged Members
        /// Implement INotifyPropertyChanged to notify the binding
        /// targets when the values of properties change.
        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                // Raise the PropertyChanged event
                this.PropertyChanged(
                     this,
                     new PropertyChangedEventArgs(
                     propertyName));
            }
        }
        #endregion
    }
}



More information about the dba-VB mailing list