I'm new to WPF, and am genuinely trying to figure out as much as possible by myself...
I've created my first DataGrid control, and I am trying to populate it with a list of strings as below:
<UserControl x:Class="DataGridTest"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid Style="{StaticResource ContentRoot}">
<DataGrid Name="MyDataGrid" ItemsSource="{Binding}" HorizontalAlignment="Stretch" Height="500"/>
</Grid>
</UserControl>
Private Sub DataGridTest_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
Dim StringCollection As List(Of String) = New List(Of String)
StringCollection.Add("Test")
StringCollection.Add("Test1")
StringCollection.Add("Test2")
StringCollection.Add("Test3")
StringCollection.Add("Test4")
MyDataGrid.DataContext = StringCollection
End Sub
However, it actually populates with a heading "Length" and values as the length of the strings. Why is it not populating with the string values, themselves?
It's clear that I'm missing something fundamental here, but I can't for the life of me figure out what it is.
Thanks in advance!
Because DataGrid
by default auto generates columns from given item class so it will search for properties in String
class that it can convert into column. You can avoid that by creating your own column and turning off AutoGenerateColumns
<DataGrid Name="MyDataGrid" ItemsSource="{Binding}" ... AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding}" Header="Something"/>
</DataGrid.Columns>
</DataGrid>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With