将 DataGrid 绑定到 wpf 中的列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1731854/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Binding DataGrid to List in wpf
提问by Alon Ashkenazi
I'm trying to bind a List
to a DataGrid
.
Here is the code snippet:
我正在尝试将 a 绑定List
到 a DataGrid
。这是代码片段:
public class Parson
{
public string LastName { get; set; }
public string FirstName { get; set; }
public Parson(string lastName, string firstName)
{
LastName = lastName;
FirstName = firstName;
}
}
public class Persons : List<Parson>
{
// Parameterless constructor
public Persons()
{
}
public new void Add(Person parson)
{
base.Add(parson);
}
}
the code behind:
背后的代码:
Persons persons = new Persons();
persons.Add(new Parson("New","Person");
dataGrid1.DataContext = persons;
xaml:
xml:
<my:DataGrid Name="dataGrid1"
xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit"
CanUserAddRows="True"
ItemsSource="{Binding}"
AutoGenerateColumns="False">
<my:DataGrid.Columns>
<my:DataGridTextColumn Header="First Name" Binding="{Binding Path=FirstName}"/>
<my:DataGridTextColumn Header="Last Name" Binding="{Binding Path=LastName}"/>
</my:DataGrid.Columns>
</my:DataGrid>
The result is that an empty grid is displayed! Anyone know why?
结果是显示一个空的网格!有谁知道为什么?
回答by Blake Rogers
Try setting AutoGenerateColumns = true
尝试设置 AutoGenerateColumns = true
回答by Hardryv
(Note: This answer was verified in .NET Framework v4.0):
(注意:此答案已在 .NET Framework v4.0 中得到验证):
A simple, 4-part operation...
一个简单的 4 部分操作...
- first, on your DataGrid control in the XAML mark-up I recommend you set
AutoGenerateColumns="False"
(you should be able to do this manually better than they can do it automatically) - second, on the same control set
ItemsSource="{Binding}"
, which informs the grid it will be getting it's column and row data from a source not defined at design-time. - third, construct the columns to look how you want them to look and for each bind to the column data with something like
Binding="{Binding Path=UniqueID}"
. Note that the value after Path is interpolated so mind things like case-sensitivity. For the List<> you are targetting the value will presumably be one of the property-names from objects in your target List<>. Rinse and repeat as many times as needed for each column in your DataGrid. - fourth, in your form code-behind, within the constructor or during form load or wherever it best suits your needs, set the DataContext of your grid. This should take a form similar to
{gridControlName}.DataContext = {target-List<>}
- 首先,在 XAML 标记中的 DataGrid 控件上,我建议您设置
AutoGenerateColumns="False"
(您应该能够更好地手动执行此操作,而不是自动执行此操作) - 其次,在同一个控件集上
ItemsSource="{Binding}"
,它通知网格它将从在设计时未定义的源获取它的列和行数据。 - 第三,构建列以查看您希望它们的外观,并为每个绑定到列数据使用类似
Binding="{Binding Path=UniqueID}"
. 请注意,Path 之后的值是内插的,因此请注意区分大小写之类的事情。对于您所针对的 List<>,该值可能是目标 List<> 中对象的属性名称之一。根据需要为 DataGrid 中的每一列冲洗并重复多次。 - 第四,在您的表单代码隐藏中,在构造函数中或在表单加载期间或任何最适合您需要的地方,设置网格的 DataContext。这应该采用类似于
{gridControlName}.DataContext = {target-List<>}
When the form is loaded its grid should auto-populate with content from objects in the your List<>.
当表单加载时,它的网格应该自动填充来自 List<> 中对象的内容。
回答by Mark Carpenter
Try setting the ItemsSource instead of DataContext, and remove the ItemsSource={Binding}
from your XAML. That may do the trick.
尝试设置 ItemsSource 而不是 DataContext,并ItemsSource={Binding}
从 XAML 中删除。这可能会奏效。
Edit:
编辑:
I just checked some code I wrote that uses the same DataGrid control (WPF Toolkit), and I do in fact set the ItemsSource instead of DataContext. If you need an example, let me know.
我刚刚检查了一些我编写的使用相同 DataGrid 控件(WPF Toolkit)的代码,实际上我确实设置了 ItemsSource 而不是 DataContext。如果你需要一个例子,让我知道。
回答by ChrisF
In your DataGrid try:
在您的 DataGrid 中尝试:
ItemsSource="{Binding Path=.}"
This has worked sucessfully for me.
这对我来说很成功。