请教关于datagrid.itemsource= ObservableCollection问题

海涵德 2013-12-14 10:37:57
请教关于datagrid.itemsource= ObservableCollection问题
问题:当在代码中修改ObservableCollection中的记录后,datagrid并不能及时的显示出来记录的改变,只有通过改变datagrid的大小等相关操作后,改变的值才能逐渐随着datagrid的变化显示出来。
请问怎样才能让修改和的记录及时在datagrid中的显示出来?

...全文
301 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
Bonjour-你好 2013-12-14
  • 打赏
  • 举报
回复
person类要实现INotifyPropertyChanged接口吧。
海涵德 2013-12-14
  • 打赏
  • 举报
回复
第一次执行后 按下update按钮后,在把页面缩小 反复放大缩小页面后,一些改变的记录已经显示出来了
海涵德 2013-12-14
  • 打赏
  • 举报
回复
为简化构造一个类
<UserControl xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"  x:Class="SilverlightApplication1.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">
        <sdk:DataGrid Name="datagrid_persons" Loaded="datagrid_persons_Loaded" ></sdk:DataGrid>
        <Button Name="button_update" Content="update" VerticalAlignment="Bottom" HorizontalAlignment="Center" Click="button_update_Click" Margin="180,0,176,131"></Button>
    </Grid>
</UserControl>
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;

namespace SilverlightApplication1
{
    public partial class MainPage : UserControl
    {
        public class person 
        {
            public string name { get; set; }
            public int age { get; set; }
            public string sex { get; set; }
        }
        private ObservableCollection<person> persions = new ObservableCollection<person>();
        

        /// <summary>
        /// 
        /// </summary>
        public MainPage()
        {
            InitializeComponent();
        }

        private void datagrid_persons_Loaded(object sender, RoutedEventArgs e)
        {
            for (int i = 0; i < 10; i++) 
            {
                person p = new person() { name = string.Format("{0}{1}", "Name", i+20), age = i + 20, sex = "male" };
                persions.Add(p);
            }
            datagrid_persons.ItemsSource = persions;
        }

        private void button_update_Click(object sender, RoutedEventArgs e)
        {
            foreach (person r in persions) 
            {
                r.sex = "female";
                r.name = r.name + r.sex;
            }
        }
    }
}
当按完update按钮后,datagrid_person中数据并不改变,改变窗口大小后会逐渐显示出来。
小猪八Q 2013-12-14
  • 打赏
  • 举报
回复
使用ObservableCollection的集合,在集合变更时,会有通知的,而你的这个现象确实很神奇
Bonjour-你好 2013-12-14
  • 打赏
  • 举报
回复
“改变datagrid大小,才逐渐显示出变化”?!很神奇,不知能否提供demo?
海涵德 2013-12-14
  • 打赏
  • 举报
回复
搞定了,谢谢VagGrant
 public class person : INotifyPropertyChanged
        {
            private string _name;

            public string name { get{return _name ;} set{ _name =value;NotifyPropertyChanged ("name");} }
            public int age { get; set; }
            public string sex { get; set; }

            public event PropertyChangedEventHandler PropertyChanged;

            private void NotifyPropertyChanged(String info)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(info));
                }
            }

        }
海涵德 2013-12-14
  • 打赏
  • 举报
回复
引用
至于怎么实现该接口。应该不需要我们说了吧
最好给出代码。
VagGrant 2013-12-14
  • 打赏
  • 举报
回复
ObservableCollection类只是在添加删除行时会自动更新前台UI,你再仔细看下这个类的介绍 如果只是改变某个行的属性的话,你需要给这个类实现INotifyPropertyChanged接口。 至于怎么实现该接口。应该不需要我们说了吧。

8,737

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧