关于silverlight中如何更新(增删改)集合ItemsSource后更新到UI(Listbox、DataGrid等)

具体方案为:将ItermsSource设置为实现 INotifyCollectionChanged 接口的对象,以使集合的更改在 ItemsControl 中反映出来。

ObservableCollection<T> 类即定义这样的对象,不要贪图方便而使用List<T>。

 

请看Listbox 添加删除演示

 XAML:

< UserControl  x:Class ="ListBoxDemo.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" >

     < Grid  x:Name ="LayoutRoot"  Background ="White" >
         < ListBox  Height ="200"  Width ="200"   x:Name ="listBox"  ItemsSource =" {Binding} "  DisplayMemberPath ="Name" ></ ListBox >
         < Button  Content ="增加"  Height ="23"  HorizontalAlignment ="Left"  Margin ="11,270,0,0"  Name ="buttonAdd"  VerticalAlignment ="Top"  Width ="75"  Click ="buttonAdd_Click"   />
         < Button  Content ="删除"  Height ="23"  HorizontalAlignment ="Left"  Margin ="100,270,0,0"  Name ="buttonRemove"  VerticalAlignment ="Top"  Width ="75"  Click ="buttonRemove_Click"   />
     </ Grid >
</ UserControl >

CS:

using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Collections.ObjectModel;
namespace ListBoxDemo
{
     public  partial  class MainPage : UserControl
    {
         public MainPage ()
        {
            InitializeComponent ();
            BindingData ();
        }
        ObservableCollection<User> myUsers =  new ObservableCollection<User> (); // 数据源
         private  void BindingData ()
        {
            myUsers.Clear ();
            myUsers.Add ( new User ( " A1 "" A11 "));
            myUsers.Add ( new User ( " A2 "" A12 "));
            myUsers.Add ( new User ( " A3 "" A13 "));
            myUsers.Add ( new User ( " A4 "" A14 "));
            myUsers.Add ( new User ( " A5 "" A15 "));
             this.listBox.ItemsSource = myUsers; // 绑定数据源
        }

         private  void buttonAdd_Click ( object sender, RoutedEventArgs e)
        {
            myUsers.Add ( new User ( " A " + (myUsers.Count +  1),  " A1 " + myUsers.Count +  1));
             // this.listBox.ItemsSource = myUsers;
        }

         private  void buttonRemove_Click ( object sender, RoutedEventArgs e)
        {
             if (myUsers.Count >  0)
                myUsers.RemoveAt (myUsers.Count -  1);
             // this.listBox.ItemsSource = myUsers;
        }
    }
     public  class User
    {
         public User ( string name,  string address)
        {
            Name = name;
            Address = address;
        }
         public  string Name {  getset; }
         public  string Address {  getset; }
    }
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值