`

wpf学习之ObservableCollection<T>相关知识

 
阅读更多
本篇学习了ObservableCollection<T>相关知识,因为在项目开发中我碰到一些问题,后来发现时我的理解偏差!所以做下笔记!

(一)代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
//添加命名空间
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace CatalogTest
{
    public partial class ObservableCollectionVSList : UserControl
    {
        ObservableCollection<Student> students = new ObservableCollection<Student>();
        //List<Student> students = new List<Student>();
        Student selectedStudent = null;

        public ObservableCollectionVSList()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(ObservableCollectionVSList_Loaded);
        }

        void ObservableCollectionVSList_Loaded(object sender, RoutedEventArgs e)
        {
            Student student1 = new Student() { StudentID = "001", StudentName = "张三" };
            Student student2 = new Student() { StudentID = "002", StudentName = "李四" };
            Student student3 = new Student() { StudentID = "003", StudentName = "王五" };
            students.Add(student1);
            students.Add(student2);
            students.Add(student3);
            //绑定
            listBox1.ItemsSource = students;
            listBox1.DisplayMemberPath = "StudentName";
            //注册选择项事件
            this.listBox1.SelectionChanged += new SelectionChangedEventHandler(listBox1_SelectionChanged);           

        }      

        void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {            
            selectedStudent = this.listBox1.SelectedItem as Student;            
        }

        //删除
        private void btnDel_Click(object sender, RoutedEventArgs e)
        {
            if (selectedStudent != null)
            {
                students.Remove(selectedStudent);
            }          

        }
        //修改
        private void btnEdit_Click(object sender, RoutedEventArgs e)
        {
            if (selectedStudent != null)
            {
                int myIndex = students.IndexOf(selectedStudent);
                students[myIndex].StudentName = "我改名了!";
            }
        }
        //添加
        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            Student student = new Student() { StudentID="009",StudentName="Joetao"};           
            students.Add(student);
        }
    }
    public class Student
    {
        public string  StudentID { get; set; }
        public string  StudentName { get; set; }
    }   

    //分别采用 ObservableCollection<Student>与List<Student>作为绑定数据源
    //当我们用List<T>作为数据源绑定UI控件时:当做增删改操作来修改students绑定数据源时,数据源都不能通知UI更新。
    //当我们用ObservableCollection<T>作为数据绑定UI控件时:当做增删改操作来修改students绑定数据源时,Add()和Remove()操作修改的数据源能通知UI更新,而改操作不能更新UI.
    //                               //这一点正说明了MSDN上对ObservableCollection<T>类介绍:"表示一个动态数据集合,在添加项、移除项或刷新整个列表时,此集合将提供通知。"
   
    //我在处理这个问题理解偏差:
    //在一开始的时候我对这句话:“实现您自己的集合之前,应考虑使用 ObservableCollection<T> 类,该类具有 INotifyCollectionChanged 和 INotifyPropertyChanged 的内置实现。“
    //被我理解为了只要用了ObservableCollection<T>,这个类集合以及类集合中的所有成员属性就具有更改通知的功能,这个理解是错误。ObservableCollection<T>只是针对T类型而言,并非给予了
    //类成员属性更改通知的功能,要想类成员属性具有更改UI功能还得让类继承INotifyPropertyChanged接口,并用 (NotifyCollectionChangedEventArgs) 的事件数据报告有关集合更改的特性的信息
    }


(二)实现类成员更改通知UI:

     要想实现类属性值修改,我们必须修改Student类,如下:

    public class Student : INotifyPropertyChanged
    {
        private string studentID;
        public string StudentID
        {
            get { return studentID; }
            set
            {
                studentID = value;
                NotifyPropertyChange("StudentID");
            }
        }
        private string studentName;
        public string StudentName
        {
            get { return studentName; }
            set
            {
                studentName = value;
                NotifyPropertyChange("StudentName");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChange(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

    }

(三)总结:
          本篇学习了 ObservableCollection<T>与List<T>作为绑定数据源的不同,实例充分说明了 ObservableCollection<T>在Silverlight中作为绑定数据源的优势! 并例举了自己最初对ObservableCollection<T>的误解。并说明了怎样实现类属性成员的修改更改UI的实现方法!这里绑定可以是OneTime,OneWay,TwoWay.具体    理解可以看我的另一篇文章Silverlight中OneTime,OneWay,TwoWay及INotifyPropertyChanged 接口的理解

(四)
数据层已经变了,为何有时更新不了界面,因为取不到界面

(五)
线程内进行
for (int i = 0; i < vCancels.Count; i++)
                {
                    Mgr.TransactionService.CancelOrder("F", vCancels[i].OrderId, vCancels[i].ContractNo, vCancels[i].OperatorNo);
                }


不要涉及到任何界面的,开始用界面的iTransactionService
iTransactionService = Mgr.TransactionService;
线程中for循环还是会卡界面
分享到:
评论

相关推荐

    C#+List+GridControl实现主从表嵌套

    C#+List&lt;T&gt;+GridControl实现主从表嵌套

    WPF控件范例

    &lt;br&gt;AnimatingTilePanel&lt;br&gt;BlockBar&lt;br&gt;ColorPicker&lt;br&gt;DateControls&lt;br&gt;FlipTile3D&lt;br&gt;FolderPicker&lt;br&gt;Graph&lt;br&gt;Hex&lt;br&gt;InfoTextBox&lt;br&gt;...VisualTreeViewer&lt;br&gt;XAMLTShirt&lt;br&gt;Zap&lt;br&gt;&lt;br&gt;下载自http://wpf.netfx3.com/&lt;br&gt;版权归原作者所有

    WPF 实现缩放移动

    WPF &lt;ZoomableCanvas&gt; 实现缩放移动

    pro wpf with vb2008

    &lt;br&gt;&lt;br&gt;Developers encountering WPF and .NET 3.5 for the first time in their professional lives.&lt;br&gt;About the Apress Pro Series&lt;br&gt;&lt;br&gt;The Apress Pro series books are practical, professional tutorials...

    Windows Presentation Foundation Unleashed (2007).rar

    2006 年12月 &lt;br&gt;【开 本】 16开 &lt;br&gt;【页 码】 656 &lt;br&gt;【版 次】1-1 &lt;br&gt;Book Description&lt;br&gt;&lt;br&gt; &lt;br&gt;&lt;br&gt;Printed entirely in color, with helpful figures and syntax coloring to make code samples appear ...

    WCF & WPF 聊天程序源码

    WCF & WPF 聊天程序源码&lt;br&gt;For those that have read some of my other CodeProject articles you will probably know, that I am not shy about trying out new technologies. But one good thing about that is ...

    WPF DataGrid 分组演示程序

    微软官网 WPF DataGrid 分组演示程序 内容包括 DataGrid,ObservableCollection&lt;T&gt;,ICollectionView,CollectionViewSource等模块的应用

    Wpf控件应用小程序

    &lt;RadioButton&gt;中国&lt;/RadioButton&gt; &lt;RadioButton&gt;美国&lt;/RadioButton&gt; &lt;RadioButton&gt;日本&lt;/RadioButton&gt; &lt;RadioButton&gt;法国&lt;/RadioButton&gt; &lt;/StackPanel&gt; &lt;/Expander&gt; &lt;Expander Name="expander2" IsExpanded=...

    wpf控制listView(gridview)列显示与隐藏

    Page1的ListView是别人的代码。... ObservableCollection&lt;DataGridColumn&gt; collec=new ObservableCollection&lt;DataGridColumn&gt; ();我们控件不是listview类型的,所以不能 public GridViewColumnCollection collec;。

    用MVVM架构实现的计算器小程序实例,供WPF和MVVM初学者参考.

    /// &lt;summary&gt; /// 加法命令 /// &lt;/summary&gt; public BaseCommand AddCommand { get; set; } /// &lt;summary&gt; /// 减法命令 /// &lt;/summary&gt; public BaseCommand SubCommand { get; set; } /// &lt;summary&gt; /// ...

    Professional.WPF.Programming

    From creating appealing graphics and animated structures to enhancing performance and security, you'll be programming in no time.&lt;br&gt;&lt;br&gt;First you'll explore the WPF framework and learn how to ...

    修复: NullReferenceException 发生异常时运行基于.NET Framework 4.0 的 WPF 应用程序包含在嵌套模板中的事件

    &lt;ListBox ItemsSource="{StaticResource Data}" &gt; &lt;ListBox.ItemTemplate&gt; &lt;DataTemplate&gt; &lt;ListBox ItemsSource="{Binding .}"&gt; &lt;ListBox.ItemTemplate&gt; &lt;DataTemplate&gt; &lt;TextBlock Text="{Binding .}" ...

    UniCAD - Practical WPF Graphics Programming (Nov 2007)

    《实用WPF图形编程》&lt;br&gt;英文PDF

    C#WPF 右键菜单 显示 事件触发 测试通过

    C#WPF 右键菜单 显示 事件触发 测试通过 &lt;TextBox Name="txtbox"&gt; &lt;!-- 设置右键菜单 --&gt; &lt;TextBox.ContextMenu&gt; &lt;ContextMenu&gt; &lt;MenuItem Header="编辑"&gt; &lt;!-- 调用系统命令 --&gt; &lt;MenuItem Command="Copy"&gt;&lt;/...

    <<WPF揭秘>>

    详细介绍了WPF的相关技术,是很好的入门书籍。

    WPF 开发实例 (VS2008)

    &lt;StackPanel Height="Auto" Name="stackPanel1" Width="Auto" &gt; &lt;ListView Height="Auto" x:Name="listView1" Width="Auto" ItemsSource="{Binding}" SelectionChanged="Equipment_SelectionChanged" ...

    WPF揭秘_code

    WPF揭秘_code.rar WPF揭秘源码收录,赞!!!

    WPF在线订单系统-其他

    &lt;p&gt;WPF 做的一个简易订单系统,采用了Caliburn和Telerik结合的方式,可以作为一些开发的参考.&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;软件架构&lt;/p&gt;&lt;p&gt;软件架构说明 基于Caliburn.Micro 框架 包含了NPOI + Dapper 等库&lt;/p&gt;&lt;p&gt;报表采用嵌入Web...

    WPF弹出带蒙板的消息框

    /// &lt;param name=message&gt;消息&lt;/param&gt; /// &lt;param name=owner&gt;父级窗体&lt;/param&gt; public static void ShowDialog(string message, Window owner) { //蒙板 Grid layer = new Grid() { Backgrou

    WPF在DataGrid列中使用ComboBox绑定,改变ComboBox可回传至绑定的实体,ComboBox绑定的是一个实体

    WPF在DataGrid列中使用ComboBox绑定,改变ComboBox可回传至绑定的实体, 网上找了好久,终于搞定了,不是那个用枚举的例子!!ComboBox绑定的是一个实体列表. 程序名字叫 给唐僧选老婆!哈哈

Global site tag (gtag.js) - Google Analytics