8,735
社区成员
发帖
与我相关
我的任务
分享
<DataGrid AutoGenerateColumns="False" RowHeaderWidth="20" FontSize="12" x:Name="DataGrid4" RowBackground="AliceBlue" AlternatingRowBackground="White" RowHeight="25" HorizontalGridLinesBrush="Green" VerticalGridLinesBrush="Green" Grid.Column="1" HorizontalAlignment="Left" Height="199" Margin="57.8,61,0,0" VerticalAlignment="Top" Width="532">
<DataGrid.Columns>
<DataGridTextColumn Header="测段起点" Width="70" Binding="{Binding 测段起点, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" ></DataGridTextColumn>
<DataGridTextColumn Header="测段终点" Width="85" Binding="{Binding 测段终点,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></DataGridTextColumn>
<DataGridTextColumn Header="测段长度(m)" Width="85" Binding="{Binding 测段长度,Mode=TwoWay ,UpdateSourceTrigger=PropertyChanged}"></DataGridTextColumn>
<DataGridTextColumn Header="测站个数" Width="85" Binding="{Binding 测站个数,Mode=TwoWay , UpdateSourceTrigger=PropertyChanged}" ></DataGridTextColumn>
<DataGridTextColumn Header="观测高差(m)" Width="85" Binding="{Binding 观测高差, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></DataGridTextColumn>
<DataGridTextColumn Header=" 高差改正数(m)" Width="100" Binding="{Binding correctionheiht,Mode=TwoWay ,UpdateSourceTrigger=PropertyChanged}"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
private void Button_Click_2(object sender, RoutedEventArgs e)
{
CaResult caResult = new CaResult();
double[] result = caResult._correction(dispersion, K_heihht, length);
for (int i = 0; i < 5; i++)
{
ps3.correctionheiht = Convert.ToString(result[i]);
infolist2.Add(ps3);
}
DataGrid4.ItemsSource = infolist1;
}
getDatas infolist1 = new getDatas();
getData ps2 = new getData();
public class Person : INotifyPropertyChanged
{
public int ID { set; get; }
public string Name { set; get; }
public int Birthday { set; get; }
private int age;
//注意这里特意追加了 OnPropertyChanged事件属性。
public int Age
{
get { return age; }
set
{
if (age != value)
{
age = value;
OnPropertyChanged("Age");
}
}
}
#region INotifyPropertyChanged Members
/// <summary>
/// INotifyPropertyChanged requires a property called PropertyChanged.
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// Fires the event for the property when it changes.
/// </summary>
public virtual void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
}
然后主程里的代码是:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
//初始化人名列表
getPeopleData();
//绑定数据源
DataGrid_List.ItemsSource = People;
}
//这句意思是先在内存中占个位。
public List<Person> People = new List<Person>();
//按钮触发方法
private void button_Click(object sender, RoutedEventArgs e)
{
var p_q = People.SingleOrDefault(o => o.ID == 1);
if (p_q != null)
{
//这里改变了的值并没能在DataGrid 中体现。
p_q.Name = "张良";
//这里改变了的值会在DataGrid 中体现。为什么?不理解就再去看一下Person里的属性。
p_q.Age = 100;
}
}
//初始人物列表
private void getPeopleData()
{
Person p1 = new Person();
p1.ID = 1;
p1.Name = "张学友";
p1.Birthday = 1961;
People.Add(p1);
Person p2 = new Person();
p2.ID = 2;
p2.Name = "张柏芝";
p2.Birthday = 1980;
People.Add(p2);
Person p3 = new Person();
p3.ID = 3;
p3.Name = "张家辉";
p3.Birthday = 1967;
People.Add(p3);
DateTime dt = DateTime.Now;
foreach(var p in People)
{
//计算年龄,给列表项重新赋值
p.Age = dt.Year - p.Birthday;
}
}
}
自我觉得这个示例应该能够很完美地解决你这个贴的疑问。