使用数据绑定填充数据网格列

时间:2018-11-02 11:12:20

标签: wpf vb.net xaml datagrid

我有一个网格:

                    <DataGrid ItemsSource="{Binding}">
                        <DataGrid.Columns>
                            <DataGridTextColumn Header="Registration" Width="Auto" Binding="{Binding Registration}"/>
                            <DataGridTextColumn Header="Type" Width="Auto" Binding="{Binding Type}"/>
                        </DataGrid.Columns>
                    </DataGrid>

我有以下来源的注册和类型列表:

Class MyListPage

Public Shared _item As New List(Of ItemType)()

Dim registrations As List(Of String) = New List(Of String)()
Dim types As List(Of String) = New List(Of String)()

Public Sub New()
    InitializeComponent()

End Sub

'Code in here to fill the two lists
End Class

Public Class ItemType
    Public _reg As String
    Public _type As String

    Public Property Registration() As String
    Get
        Return _reg
    End Get
    Set(ByVal value As String)
        _reg = value
    End Set
    End Property

    Public Property Type() As String
    Get
        Return _type
    End Get
    Set(ByVal value As String)
        _type = value
    End Set
End Property
End Class

我想做的并且一直在尝试做的是将注册和类型绑定到我拥有的两个datagrid列,并使用一个可观察的集合(我想这就是我想要的)来绑定到正在更新的它们动态地

但是我绝对不知该从何去何从。注册和类型都是大约10个元素的列表,显然需要在它们各自的列中输入。这对我来说是全新的,因此将不胜感激

我很希望能够随着列表的填写一一填写。

1 个答案:

答案 0 :(得分:0)

公共属性返回ObservableCollection(Of ItemType)并绑定到该属性:

Class MyListPage

    Public Property Items As ObservableCollection(Of ItemType)()

    Public Sub New()
        InitializeComponent()
        DataContext = Me
        'Populate the Items property here...
    End Sub
End Class

XAML:

<DataGrid ItemsSource="{Binding Items}">
相关问题