I’m trying to fill a datagrid in WPF the same way I would fill it in Silverlight, but apparently it doesn’t really work the same way.
This is the Grid in XAML:
<DataGrid x:Name="gvEintraege"
CanUserAddRows="False"
CanUserDeleteRows="False"
IsReadOnly="True"
AutoGenerateColumns="True" >
<DataGrid.Columns>
<DataGridTextColumn Header="Datum"
Width="100"
Binding="{Binding datum}"/>
<DataGridTextColumn Header="Gewicht"
Width="70"
Binding="{Binding gewicht}"/>
<DataGridTextColumn Header="Muskelmasse"
Width="80"
Binding="{Binding muskelmasse}"/>
<DataGridTextColumn Header="Fettmasse"
Width="70"
Binding="{Binding fettmasse}"/>
<DataGridTextColumn Header="BMI"
Width="40"
Binding="{Binding bmi}"/>
<DataGridTextColumn Header="FFMI"
Width="50"
Binding="{Binding ffmi}"/>
<DataGridTextColumn Header="KFA"
Width="50"
Binding="{Binding kfa}"/>
</DataGrid.Columns></DataGrid>
And this is the entire codebehind:
Public Class LogWindow
Dim gvEintraege_Itemssource As New ObservableCollection(Of clsGridEintrag)
Public globaleVariablen As Application
Public Sub New()
InitializeComponent()
Me.globaleVariablen = Application.Current
Me.getLogEintraege()
Me.gvEintraege.ItemsSource = Me.gvEintraege_Itemssource
End Sub
Private Sub getLogEintraege()
Try
Dim myLog As clsLog = clsSerializer.DeSerialize(Me.globaleVariablen.standardPfadLogs & "\" & Me.globaleVariablen.aktiverUser & ".xml", New clsLog)
With myLog
For Each x In .eintraege
Me.gvEintraege_Itemssource.Add(New clsGridEintrag(x.datum, x.gewicht, x.kfa, x.muskelmasse, x.fettmasse, x.ffmi, x.bmi))
Next
End With
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Public Class clsGridEintrag
Public datum As Date
Public gewicht As Decimal
Public kfa As Decimal
Public muskelmasse As Decimal
Public fettmasse As Decimal
Public ffmi As Decimal
Public bmi As Decimal
Public Sub New()
End Sub
Public Sub New(datum_ As Date, gewicht_ As Decimal, kfa_ As Decimal, muskelmasse_ As Decimal, fettmasse_ As Decimal, ffmi_ As Decimal, bmi_ As Decimal)
Me.bmi = bmi_
Me.datum = datum_
Me.fettmasse = fettmasse_
Me.ffmi = ffmi_
Me.gewicht = gewicht_
Me.kfa = kfa_
Me.muskelmasse = muskelmasse_
End Sub
End Class
End Class
The grid creates a row for every item in the itemssource, but it doesn’t show any data in the columns. It looks like it should work this way, but apparently I’ve made a mistake somewhere. I’m pulling my hair out over this because it seems so easy, yet it won’t work.
I think you aren’t seeing anything because you’re not using public properties or dependency properties. Binding only works on one of those 2. Since you are trying to bind to public variables, it won’t work. I’m pretty sure the debug output will also warn you that the bindings cannot be found.
Otherwise, with “Autogenerate Columns” set to true, you don’t have to specify the columns. The grid should be able to read your object (bind it to an observablecollection containing your custom objects) and generate one column for each public dependency property or public property (which means they have get/set accessors).
And the next answer is right as well, you forgot to set the datacontext 😉
Public Property myCollection As New ObservableCollection<clsGridEintrag>()myCollectionmyCollection.Add(myObject)).I’m not on my coding computer, but the above should give you the results you want if you’re using AutogenerateColumns = true. The DataGrid will name each header after the name of each public property in your clsEntraig object and should list them correctly.
Here’s some additional information, remember that “Customers” is probably an ObservableCollection that contains “Customer” objects. In the “Customer” objects, the variables we want to see in the DataGrid are most likely set as “Public Property” (in C#, the equivalent is something like
public string myString {get; set;}– if you see get/set, it’s a property). Good luck!