我试图在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中的每个项创建一行,但它不会在列中显示任何数据。看起来它应该以这种方式工作,但显然我在某个地方犯了错误。我把头发拉过来因为它看起来很容易,但它不起作用。
答案 0 :(得分:1)
我认为你没有看到任何东西,因为你没有使用公共属性或依赖属性。绑定仅适用于其中一个2.由于您尝试绑定到公共变量,它将无法工作。我很确定调试输出还会警告你无法找到绑定。
否则,将“Autogenerate Columns”设置为true,则不必指定列。网格应该能够读取您的对象(将其绑定到包含您的自定义对象的observablecollection)并为每个公共依赖属性或公共属性生成一列(这意味着它们具有get / set访问器)。
下一个答案也是对的,你忘了设置datacontext;)
Public Property myCollection As New ObservableCollection<clsGridEintrag>()
myCollection
myCollection.Add(myObject)
)。我不在我的编码计算机上,但是如果你使用的是AutogenerateColumns = true,上面应该会给你你想要的结果。 DataGrid将为clsEntraig对象中每个公共属性的名称后面的每个标题命名,并且应该正确列出它们。
Here's some additional information,请记住“Customers”可能是包含“Customer”对象的ObservableCollection。在“Customer”对象中,我们希望在DataGrid中看到的变量很可能设置为“公共属性”(在C#中,等价物类似于public string myString {get; set;}
- 如果您看到get / set,则它是属性)。祝你好运!
答案 1 :(得分:0)
您尚未设置DataContext
属性,因此绑定不起作用...
在 LogWindow :
中Public Sub New()
Me.DataContext = Me.gvEintraege ' or wherever your data is
InitializeComponent()
....
End Sub