WPF DataGrid ItemsSource Issue

Please let me know that I am relatively new to WPF. I create a new ObservableCollection with the type of my simple data class and assign it to the ItemsSource property of my DataGrid . Before I get 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> "# FFA4CFF2" BorderBrush = "# FF7C7C7C" HorizontalGridLinesBrush = "White" PreviewKeyDown = "addressGrid_PreviewKeyDown" CellEditEnding = "addressGrid_CellEditEnding" BeginningEdit = "addressGrid_BeginningEdit" PreparingCellForEdit = "addressGrid_PreparingCellForEdit"> <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; 

Additional information about the population method:

I create this solution using the ArcGIS framework, so some things are not "System" ways.

  • m_pFields - an object of the IFields interface that allows me to store information about the spatial layer

  • IFields has a FieldCount property that returns the number of fields in the collection.

  • DisplayedValueForRow(i) calls another ArcGIS obj method IPropertySet.GetProperty() and returns a value.


Problem:

Everything is filled as it should, but for some odd reason, it again creates three columns (Name, Value, Index) ON ON, filling in the created in XAML - in turn, ending with 2 sets of identical data. I found this to be a weird behavior, as I swear I saw people getting attached to their net before.

What am I doing wrong?

Edit

Thanks to ChrisO's comment, I found out that there is a property called "AutoGenerateColumns" that needs to be disabled. Well, I feel like a heel. Thanks!

+6
source share
2 answers

Just set the AutoGenerateColumns property to false on the DataGrid . Then it will use only the columns you specify.

+9
source

Just set its AutoGenerateColumns to False . By default, this 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 custom columns, set AutoGenerateColumns to false and write individual columns in XAML.

+1
source

Source: https://habr.com/ru/post/943759/


All Articles