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

Silverlight Telerik控件学习:GridView双向绑定,第1张

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

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

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

Xaml示例:

+ View Code ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 <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" 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" x:name="btnSave" Click="btnSave_Click">全部保存</telerik:Radbutton>     </StackPanel> </UserControl>
Xaml.cs部分 + View Code ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 @H_404_976@ 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 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 @H_404_976@         {             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"));                 }             }         }         } }
代码比较简短,就懒得写注释了

作者: 菩提树下的杨过
出处: http://yjmyzz.cnblogs.com 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。 总结

以上是内存溢出为你收集整理的Silverlight Telerik控件学习:GridView双向绑定全部内容,希望文章能够帮你解决Silverlight Telerik控件学习:GridView双向绑定所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1010028.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存