[dba-VB] SCRUM/WPF Data Binding

Salakhetdinov Shamil mcp2004 at mail.ru
Tue Jun 2 19:03:11 CDT 2009


Hi All,

Here is the next sampple on WPD databinding - now with a combobxo to navigate between three object instances. You can also edit fields values via wpf form and you can find they are saved in form's local memory:

Here is the link to the live sample:

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

and in P.S. you can fiund code.

Basically that's all main information on databinding I wanted to tell here, and in the following samples I plan to implement step by step navigation and data manipulation set of buttons.

Thank you.

--
Shamil

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="10"/>
            <RowDefinition Height="30"/>
            <RowDefinition Height="30"/>
            <RowDefinition Height="30"/>
        </Grid.RowDefinitions>
        <TextBlock
            Margin="4" Text="Shippers:"
            Grid.Row="0" Grid.Column="0"
            VerticalAlignment= "Center"/>
        <ComboBox x:Name="cboShippers"
            Margin="4" 
            Grid.Row="0" Grid.Column="1" 
            SelectedValuePath="Id" 
            DisplayMemberPath="Name" 
            SelectionChanged="cboShippers_SelectionChanged" />
        <TextBlock
            Margin="4" Text="Id"
            Grid.Row="2" Grid.Column="0"
            VerticalAlignment= "Center"/>
        <TextBlock
            Text="{Binding Path=Id, Mode=OneWay}"
            Margin="4" 
            Grid.Row="2" Grid.Column="1"/>
        <TextBlock
            Margin="4"
            Text="Name"
            Grid.Row="3" Grid.Column="0"
            VerticalAlignment="Center"/>
        <TextBox
            Margin="4"
            Text="{Binding Path=Name, Mode=TwoWay}"
            Grid.Row="3" Grid.Column="1"/>
        <TextBlock
            Margin="4"
            Text="Phone"
            Grid.Row="4" Grid.Column="0"
            VerticalAlignment="Center"/>
        <TextBox
            Margin="4"
            Text="{Binding Path=Phone, Mode=TwoWay}"
            Grid.Row="4" Grid.Column="1" />
    </Grid>

</Page>

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

using System.Collections.Generic;
using System.Windows.Controls;
using System.Windows.Documents;

using System.ComponentModel;

namespace WpfBrowserApplication1
{
    public partial class ShippersPage : Page
    {
        private List<Shipper> _shippers;
        private Shipper _currentShipper;
        public ShippersPage()
        {
            InitializeComponent();

            _loadInProgress = true;

            _shippers = Shipper.GetAll();
            // Set the DataContext 
            // to a Shipper object
            this.DataContext = _shippers[0];
            foreach (Shipper shipper in _shippers)
                cboShippers.Items.Add(shipper);

            _currentShipper = _shippers[0];
            cboShippers.SelectedItem = _currentShipper;

            _loadInProgress = false;
        }

        private bool _loadInProgress;
        private void cboShippers_SelectionChanged(
            object sender, 
            SelectionChangedEventArgs e)
        {
            if (_loadInProgress) return;
            ComboBox cbo = (ComboBox)sender; 
            _currentShipper = (Shipper)cbo.SelectedItem;

            this.DataContext = cbo.SelectedItem;
            e.Handled = true;
        }
    }

    public class Shipper : INotifyPropertyChanged
    {
        public static List<Shipper> GetAll()
        {
            List<Shipper> list = new List<Shipper>();
            list.Add(
                new Shipper()
                {
                    Id = 1,
                    Name = "Speedy Express",
                    Phone = "(503) 555-9831"
                });
            list.Add(
                new Shipper()
                {
                    Id = 2,
                    Name = "United Package",
                    Phone = "(503) 555-3199"
                });
            list.Add(
                new Shipper()
                {
                    Id = 3,
                    Name = "Federal Shipping",
                    Phone = "(503) 555-9931"
                });
            return list;
        }

        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
    }
}


-----Original Message-----
From: Salakhetdinov Shamil <mcp2004 at mail.ru>
To: dba-VB <dba-vb at databaseadvisors.com>
Date: Tue, 02 Jun 2009 02:17:49 +0400
Subject: [dba-VB] SCRUM/WPF Data Binding

> Hi All,
> 
> I planned here is the first sample of WPF data bidining:
> 
> http://shamils-4.hosting.parking.ru/xaml/WpfBrowserApplication2.xbap
> 
<<< snip>>>



More information about the dba-VB mailing list