Reputation: 5007
Please let it be known that I am relatively new to WPF. I am creating a new ObservableCollection
with the type of my simple data class, and assigning that to the ItemsSource
property of my DataGrid
. Before I go into my problem here is the code:
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.
m_pFields
is an IFields
interface object that allows me to store spatial layer information
IFields
has a FieldCount
property which returns a number of fields in the collection
DisplayedValueForRow(i)
calls another ArcGIS obj method IPropertySet.GetProperty()
and returns the value.
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 TOP of 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.
What am I doing wrong?
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!
Upvotes: 7
Views: 14703
Reputation: 48558
Simply set its AutoGenerateColumns
to False
. By default it is True
.
If you want to show all fields of your class. do not set any columns in XAML.
But if you want to show selective columns, then set AutoGenerateColumns
to false and write individual columns in XAML.
Upvotes: 1
Reputation: 5465
Just set the AutoGenerateColumns
property to false on your DataGrid
. Then it will only use the columns you specified.
Upvotes: 10