工作中做一个工具,需要用到DataGrid,经过查询资料 使用 ObservableCollection 做绑定,实现了后台数据增删,界面实时增减。
但是如果对一个已经存在的项,更改后台数据,则界面不会发生变化。
经过调试发现后台数据内容已经发生了变化,但是界面没有更新。后来去网上查资料发现有人讲当数据发生变化时。
将ItemsSource = null,再重新绑定。经过测试确实可以改变前台数据。
但是这种写法等于刷新了所有数据,包括没有改变的内容,如果数据量很大就会带来明显的界面卡顿,对于一个完美主义者是无法接受的。又查了会资料,突然迷糊过来,知道怎么写了,代码如下。
xmal代码如下:
<Window x:Class="DataGridTest.MainWindow"
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"
xmlns:local="clr-namespace:DataGridTest"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
<StackPanel>
<Button Content="改变内容" Click="Button_Click" Background="AliceBlue"/>
<Button Content="新增数据" Click="Button_Click_1" Background="AliceBlue"/>
<DataGrid Name="dataGrid1" IsReadOnly="True" AutoGenerateColumns="False" RowDetailsVisibilityMode="Collapsed"
CanUserReorderColumns="False" CanUserSortColumns="False" CanUserResizeRows="False" >
<DataGrid.Columns >
<DataGridTextColumn Header="Name" Binding="{Binding Name}" FontSize="22" Width="*"></DataGridTextColumn>
</DataGrid.Columns >
</DataGrid>
</StackPanel>
</Window>
cs文件
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace DataGridTest
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
ObservableCollection<people> peoplelist = new ObservableCollection<people>();
int count1 = 0;
int count2 = 0;
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
peoplelist.Add(new people
{
Name = "小明"
});
peoplelist.Add(new people
{
Name = "小红"
});
dataGrid1.ItemsSource = peoplelist;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
count1 += 1;
people data;
try
{
data = peoplelist.ElementAt(0);
}
catch (System.Exception ex)
{
return;
}
data.Name = data.Name + count1.ToString();
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
count2 += 1;
peoplelist.Add(new people
{
Name = "小红_"+count2.ToString()
});
dataGrid1.ItemsSource = peoplelist;
}
}
public class people : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private string name;
public string Name
{
get { return name; }
set
{
name = value;
if (PropertyChanged != null)
{
PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Name"));
}
}
}
}
}
效果示例
基本原理就是Wpf中只有继承了INotifyPropertyChanged类才具有通知能力,ObservableCollection具有通知能力,仅限于他的数据集合。
第一次写博客,写的不好,请多多包涵。后续有时间了,会更新下DataGrid怎样根据列表中的每行的某个内容改变整行的背景色等。