8,737
社区成员
发帖
与我相关
我的任务
分享
<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中数据并不改变,改变窗口大小后会逐渐显示出来。 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));
}
}
}