[英]Bind the ItemsSource of a DataGrid to a List
I am trying to create a binding between a List
and a DataGrid
. 我正在尝试在
List
和DataGrid
之间创建绑定。 I haven't found a working Solution in the net which is very strange. 我没有在网络上找到有效的解决方案,这很奇怪。
In my little example I create a List
of objects with the two public
properties Age
and Name
: 在我的小示例中,我创建了一个具有两个
public
属性Age
和Name
的对象List
:
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
public ObservableCollection<Person> Collection { get; set; }
public List<Person> Persons { get; set; }
private void WindowLoaded(object sender, RoutedEventArgs e)
{
this.Persons = new List<Person>();
for (int i = 0; i != 35; i++)
{
this.Persons.Add(new Person() {Age = i, Name = i.ToString()});
}
this.Collection = new ObservableCollection<Person>(this.Persons);
}
The XAML Code looks like this: XAML代码如下所示:
<Grid DataContext="{Binding ElementName=TestWindow, Path=.}">
<DataGrid x:Name="DataGrid" ItemsSource="{Binding Collection}" />
</Grid>
or this (both is not working): 或此(均不起作用):
<Grid DataContext="{Binding ElementName=TestWindow, Path=.}">
<DataGrid x:Name="DataGrid" ItemsSource="{Binding Persons}" />
</Grid>
if I use this.DataGrid.ItemsSource = this.Persons;
如果我使用
this.DataGrid.ItemsSource = this.Persons;
I see at least all the items in the list but I have to this.DataGrid.Items.Refresh()
every time the Source List
changes which is the reason why I ask the question: 我至少看到列表中的所有项目,但是每次源
List
更改时,我都必须要this.DataGrid.Items.Refresh()
,这就是我问这个问题的原因:
What am I doing wrong? 我究竟做错了什么? Do I need to implement
INotifyPropertyChanged
? 我是否需要实现
INotifyPropertyChanged
?
This question must be very easy to answer but it would be also awesome to understand the mechanics. 这个问题必须很容易回答,但是理解其原理也很棒。
After a very long day of coding I was too nervous and tired. 经过一整天的编码,我太紧张又累。 the solution is very easy:
解决方案非常简单:
Implement the INotifyPropertyChanged
Interface
for the Person
class: 为
Person
类实现INotifyPropertyChanged
Interface
:
public class Person: INotifyPropertyChanged
{
private string name;
private int age;
public string Name
{
get
{
return this.name;
}
set
{
this.name = value;
this.OnPropertyChanged("Name");
}
}
public int Age
{
get
{
return this.age;
}
set
{
this.age = value;
this.OnPropertyChanged("Age");
}
}
protected void OnPropertyChanged(string name)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
And then bind the Data with these Code behind: 然后将数据与以下代码绑定:
this.DataGrid.DataContext = this.Persons;
For a successful Binding
you need also these XAML
Code: 为了成功进行
Binding
您还需要以下XAML
代码:
<DataGrid x:Name="DataGrid" ItemsSource="{Binding}" />
The valuesin the DataGrid
will now refresh whenever the Data in one of the Person
instances changes. 现在,只要
Person
实例之一中的数据发生更改, DataGrid
的值就会刷新。
Okay, so the reason why you are having issues is because what happens when the window is loaded and how the data binding is set up. 好的,所以出现问题的原因是因为加载窗口时会发生什么以及如何设置数据绑定。
The ItemsSource of the DataGrid doesn't happen until the Window has already loaded (it's getting set in your Window loaded event). 在窗口已加载之前,DataGrid的ItemsSource不会发生(已在您的窗口加载事件中进行设置)。 So as a result, the DataGrid does not now that it's ItemsSource has changed.
因此,DataGrid现在不会更改它的ItemsSource。 You can fix this by adding INotiftyProperty changed to the list itself, or, you can create the DataGrid list first, and just populate when the Window has loaded.
您可以通过将更改后的INotiftyProperty添加到列表本身来解决此问题,或者可以先创建DataGrid列表,然后在Window加载后填充它。
For example: 例如:
xaml.cs xaml.cs
public partial class MainWindow : Window
{
public MainWindow()
{
Collection = new ObservableCollection<Person>();
InitializeComponent();
}
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
public ObservableCollection<Person> Collection { get; set; }
public List<Person> Persons { get; set; }
private void WindowLoaded(object sender, RoutedEventArgs e)
{
this.Persons = new List<Person>();
for (int i = 0; i != 35; i++)
{
this.Persons.Add(new Person() { Age = i, Name = i.ToString() });
}
foreach (var p in Persons)
{
Collection.Add(p);
}
}
}
xaml: XAML:
<Window x:Class="WpfApplication1.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"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525"
x:Name="TestWindow"
Loaded="WindowLoaded">
<Grid DataContext="{Binding ElementName=TestWindow, Path=.}">
<DataGrid x:Name="DataGrid" ItemsSource="{Binding Collection}" />
</Grid>
</Window>
I do a similar thing a ton in Windows Forms Apps with data grid views. 我在Windows Forms Apps中使用数据网格视图进行了大量类似的操作。 So maybe this can point you in the right direction... I don't have much experience with wpf.
因此,也许这可以为您指明正确的方向...我对wpf经验不足。
List<Connection> connList = new List<Connection>();
//populate list somehow
BindingSource bs = new BindingSource();
bs.DataSource = connList;
DataGrid.DataSource = bs;
public class Connection
{
public string Name {get; set;}
public string Connect {get; set;}
public Connection(string n, string c)
{
Name = n;
Connect = c;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.