我试图在 WPF 中填充数据网格,就像在 Silverlight 中填充它一样,但显然它的工作方式并不相同。
这是 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>
这是整个代码隐藏:
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
结束类
网格为 itemssource 中的每个项目创建一行,但不显示列中的任何数据。看起来它应该以这种方式工作,但显然我在某个地方犯了一个错误。我正在为此拉头发,因为它看起来很容易,但它不会起作用。