Silverlight Telerik控件学习:GridView双向绑定

做过WinForm数据库开发的人,一定有类似经历:DataGrid绑定后,如果允许行编辑,数据一顿修改后,想批量保存修改后的结果,通常是将DataGrid的所有行遍历,用FindControl找出其中的TextBox之类的控件,取值,然后处理,如果行模板中的控件变化了,可能之前的处理代码又要修改...

.Net发展到WPF/SL时代,有了双向绑定,这种痛苦经历已经一去不返了,我们只需要关注数据即可,GridView与数据源之间会相互通知各自的变化情况,批量保存时,不管GridView中的数据用户如何修改,也不用去理会行模板中的控件名是啥,直接对数据源进行处理即可。

Xaml示例:

<UserControl xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"  x:Class="SLShortCut.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">

    <StackPanel x:Name="LayoutRoot" Background="White">
        <telerik:RadGridView  ShowGroupPanel="False" AutoGenerateColumns="False" x:Name="grd1" ItemsSource="{Binding Path=LstData, Mode=TwoWay}" SelectionMode="Extended">
            <telerik:RadGridView.Columns>
                <telerik:GridViewSelectColumn  />
                <telerik:GridViewColumn Header="姓名">
                    <telerik:GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBox x:Name="txtName" Text="{Binding Name,Mode=TwoWay}" VerticalAlignment="Center" Height="22"></TextBox>
                        </DataTemplate>
                    </telerik:GridViewColumn.CellTemplate>
                </telerik:GridViewColumn>
                
                <telerik:GridViewColumn Header="年龄">
                    <telerik:GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBox x:Name="txtAge" Text="{Binding Age,Mode=TwoWay}" VerticalAlignment="Center" Height="22"></TextBox>
                        </DataTemplate>
                    </telerik:GridViewColumn.CellTemplate>
                </telerik:GridViewColumn>
                
                <telerik:GridViewColumn Header="操作">
                    <telerik:GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <telerik:RadButton Content="+" Width="20" Height="20" VerticalAlignment="Center"  x:Name="btnAdd" Click="btnAdd_Click"/>
                                <telerik:RadButton Content="-" Margin="5,0,0,0" Width="20" Height="20" VerticalAlignment="Center" x:Name="btnSub" Click="btnSub_Click"/>
                            </StackPanel>
                        </DataTemplate>
                    </telerik:GridViewColumn.CellTemplate>
                </telerik:GridViewColumn>
            </telerik:RadGridView.Columns>
        </telerik:RadGridView>
        <telerik:RadButton Height="22" Width="100"  Margin="0,10,0,0" x:Name="btnSave" Click="btnSave_Click">全部保存</telerik:RadButton>
    </StackPanel>
</UserControl>
Xaml.cs部分
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using Telerik.Windows.Controls;

namespace SLShortCut
{
    public partial class MainPage : UserControl
    {
        PersonCollection pc = new PersonCollection();

        public MainPage()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            pc.LstData.Add(new Person() { Name = "张三", Age = 1 });
            pc.LstData.Add(new Person() { Name = "李四", Age = 2 });

            this.DataContext = pc;
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            pc.LstData.Add(new Person() { });
        }

        private void btnSub_Click(object sender, RoutedEventArgs e)
        {
            var p = (sender as RadButton).DataContext as Person;
            pc.LstData.Remove(p);

        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            foreach (var item in this.pc.LstData)
            {
                MessageBox.Show(string.Format("Name:{0},Age:{1}", item.Name, item.Age));
            }
        }
    }

    public class PersonCollection 
    {
        private ObservableCollection<Person> lstData = new ObservableCollection<Person>();

        public ObservableCollection<Person> LstData
        {
            get { return lstData; }
            set { lstData = value; }
        }
    }

    public class Person : INotifyPropertyChanged
    {

        public event PropertyChangedEventHandler PropertyChanged;

        private string _name = "";

        public string Name
        {
            get { return _name; }
            set
            {
                _name = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("Name"));
                }
            }
        }

        private int _age = 0;

        public int Age
        {
            get { return _age; }
            set
            {
                _age = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("Age"));
                }
            }
        }


    }
}
代码比较简短,就懒得写注释了
posted @ 2011-06-13 11:29  菩提树下的杨过  阅读(5202)  评论(13编辑  收藏  举报