WPF DataGrid ItemsSource 问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16246492/
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
WPF DataGrid ItemsSource Issue
提问by Logan B. Lehman
Please let it be known that I am relatively new to WPF. I am creating a new ObservableCollectionwith the type of my simple data class, and assigning that to the ItemsSourceproperty of my DataGrid. Before I go into my problem here is the code:
请让我知道我对 WPF 比较陌生。我正在ObservableCollection使用我的简单数据类的类型创建一个新的,并将其分配给ItemsSource我的DataGrid. 在我进入我的问题之前,这里是代码:
XAML:
XAML:
<my:DataGrid SelectionMode="Single" SelectionUnit="Cell" Height="113" HorizontalAlignment="Left" Margin="11,22,0,0" Name="addressGrid" VerticalAlignment="Top" Width="213" Background="#FFE2E2E2" AlternatingRowBackground="#FFA4CFF2" BorderBrush="#FF7C7C7C" HorizontalGridLinesBrush="White" PreviewKeyDown="addressGrid_PreviewKeyDown" CellEditEnding="addressGrid_CellEditEnding" BeginningEdit="addressGrid_BeginningEdit" PreparingCellForEdit="addressGrid_PreparingCellForEdit">
<my:DataGrid.Columns>
<my:DataGridTextColumn Header="Name" Width="*" Binding="{Binding Path=Name}"></my:DataGridTextColumn>
<my:DataGridTextColumn Header="Value" Width="3*" Binding="{Binding Path=Value}"></my:DataGridTextColumn>
<my:DataGridTextColumn Header="Index" Visibility="Hidden" Binding="{Binding Path=Index}"></my:DataGridTextColumn>
</my:DataGrid.Columns>
</my:DataGrid>
Data Class:
数据类:
public class PropertyFields
{
public string Name { get; set; }
public object Value { get; set; }
public int Index { get; set; }
}
Population:
人口:
ObservableCollection<PropertyFields> propertyList = new ObservableCollection<PropertyFields>();
for (int i = 0; i < m_pFields.FieldCount - 1; ++i)
{
propertyList.Add(new PropertyFields() {Name = m_pFields.Field[i].AliasName, Value = DisplayedValueForRow(i), Index = i});
}
// Set ItemSource to populate grid
addressGrid.ItemsSource = propertyList;
More information about the population method:
有关人口方法的更多信息:
I am building this solution with ArcGIS framework, so some things are not "System" in ways.
我正在用 ArcGIS 框架构建这个解决方案,所以有些东西在某些方面不是“系统”。
m_pFieldsis anIFieldsinterface object that allows me to store spatial layer informationIFieldshas aFieldCountproperty which returns a number of fields in the collectionDisplayedValueForRow(i)calls another ArcGIS obj methodIPropertySet.GetProperty()and returns the value.
m_pFields是一个IFields接口对象,允许我存储空间层信息IFields有一个FieldCount属性返回集合中的多个字段DisplayedValueForRow(i)调用另一个 ArcGIS obj 方法IPropertySet.GetProperty()并返回值。
The Problem:
问题:
Everything is being populated as it should be, but for some odd reason it is re-creating the three columns (Name, Value, Index) again ON TOPof populating the ones created in XAML -- In turn ending up with 2 sets of the same data. I found this to be weird behavior, as I swear I have seen people bind to their grid this way before.
一切都按原样填充,但出于某种奇怪的原因,它在填充在 XAML 中创建的列的顶部再次重新创建了三列(名称、值、索引)——反过来又以 2 组相同的数据。我发现这是一种奇怪的行为,因为我发誓我以前见过人们以这种方式绑定到他们的网格。
What am I doing wrong?
我究竟做错了什么?
Edit
编辑
Thanks to ChrisO's comment, I found out that there is a property called "AutoGenerateColumns" that needed to be turned off. Well I feel like a heel. Thanks!
感谢 ChrisO 的评论,我发现有一个名为“AutoGenerateColumns”的属性需要关闭。嗯,我觉得自己像个高跟鞋。谢谢!
回答by ChrisO
Just set the AutoGenerateColumnsproperty to false on your DataGrid. Then it will only use the columns you specified.
只需AutoGenerateColumns在您的DataGrid. 然后它将只使用您指定的列。
回答by Nikhil Agrawal
Simply set its AutoGenerateColumnsto False. By default it is True.
只需将其设置AutoGenerateColumns为False. 默认情况下是True.
If you want to show all fields of your class. do not set any columns in XAML.
如果您想显示您班级的所有字段。不要在 XAML 中设置任何列。
But if you want to show selective columns, then set AutoGenerateColumnsto false and write individual columns in XAML.
但是,如果您想显示选择性列,则设置AutoGenerateColumns为 false 并在 XAML 中编写单个列。

