尝试将ObservableCollection数据绑定到ListView MVVM方式

时间:2017-08-17 20:03:07

标签: wpf vb.net mvvm data-binding

我是WPF / MVVM的新手,并且一直在尝试将可观察的集合类绑定到列表视图,没有代码,只有MVVM。

XAML:

<Window.DataContext>
    <local:acLengthCalcVM/>
</Window.DataContext>

<StackPanel>
    <GroupBox Header="Section(s) Selected" Margin="3,10,3,10">
        <ListView x:Name="lvSections" Margin="0,6,0,2" Height="150" ItemsSource="{Binding SectionsSelected}">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="#" Width="30"/>
                    <GridViewColumn Header="Reference Tag #" Width="100"/>
                    <GridViewColumn Header="Section Type" Width="150"/>
                    <GridViewColumn Header="Section Length" Width="100"/>
                </GridView>
            </ListView.View>
        </ListView>
    </GroupBox>
</StackPanel>

视图模型:

Public Class acLengthCalcVM
Implements INotifyPropertyChanged
Dim _sectionsSelected As ObservableCollection(Of acLengthCalcModel) = New ObservableCollection(Of acLengthCalcModel)
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged


Public Property SectionsSelected As ObservableCollection(Of acLengthCalcModel)
    Get
        Return _sectionsSelected
    End Get
    Set(value As ObservableCollection(Of acLengthCalcModel))
        _sectionsSelected = value
        NotifyPropertyChanged("SectionsSelected")
    End Set
End Property


Private Sub NotifyPropertyChanged(Optional propertyName As String = "")
    RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
End Sub 

结束班

模特:

Public Class acLengthCalcModel
Inherits ObservableCollection(Of acLengthCalcData)
Public Sub New()
    Add(New acLengthCalcData(1, "n/a", "VTU Frame", 8))
    Add(New acLengthCalcData(2, "1.1.01", "Air Conveyor Straight", 118))
End Sub

结束班

Public Class acLengthCalcData
' Constructor for this Class
Public Sub New(serialNum As Integer, refTagNum As String, sectionType As String, sectionLength As Double)
    _refTagNum = refTagNum
    _sectionLength = sectionLength
    _sectionType = sectionType
    _serialNum = serialNum
End Sub

' Reference Tag #
Private _refTagNum As String
Public Property RefTagNum As String
    Get
        Return _refTagNum
    End Get
    Set(value As String)
        _refTagNum = value
    End Set
End Property

' Section Length
Private _sectionLength As Double
Public Property SectionLength As Double
    Get
        Return _sectionLength
    End Get
    Set(value As Double)
        _sectionLength = value
    End Set
End Property

' Section Type
Private _sectionType As String
Public Property SectionType As String
    Get
        Return _sectionType
    End Get
    Set(value As String)
        _sectionType = value
    End Set
End Property

' Serial #
Private _serialNum As Integer
Public Property SerialNum As Integer
    Get
        Return _serialNum
    End Get
    Set(value As Integer)
        _serialNum = value
    End Set
End Property

结束班

当我运行代码时,列表视图仅使用其标题创建,我在模型构造函数中初始化的数据都不显示。

enter image description here

任何指针?感谢。

1 个答案:

答案 0 :(得分:1)

您应该在视图模型类中创建并返回acLengthCalcModel的实例:

Public Class acLengthCalcVM
    Implements INotifyPropertyChanged
    Dim _sectionsSelected = New acLengthCalcModel
    Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged


    Public Property SectionsSelected As acLengthCalcModel
        Get
            Return _sectionsSelected
        End Get
        Set(value As acLengthCalcModel)
            _sectionsSelected = value
            NotifyPropertyChanged("SectionsSelected")
        End Set
    End Property


    Private Sub NotifyPropertyChanged(Optional propertyName As String = "")
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
    End Sub
End Class

您还需要使用acLengthCalcData属性将每个列绑定到DisplayMemberBinding类的属性:

<ListView x:Name="lvSections" Margin="0,6,0,2" Height="150" ItemsSource="{Binding SectionsSelected}">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="#" DisplayMemberBinding="{Binding SerialNum}" Width="30"/>
            <GridViewColumn Header="Reference Tag #" DisplayMemberBinding="{Binding RefTagNum}" Width="100"/>
            <GridViewColumn Header="Section Type" DisplayMemberBinding="{Binding SectionType}" Width="150"/>
            <GridViewColumn Header="Section Length" DisplayMemberBinding="{Binding SectionLength}" Width="100"/>
        </GridView>
    </ListView.View>
</ListView>