为什么我的ItemsControl不显示我的ViewModels?

时间:2017-12-12 19:25:49

标签: wpf vb.net xaml

我正在尝试使用数据库中的项目列表填充页面。我为这些项目创建了模型和视图模型,但它们没有出现在我的ItemsControl中。

我有一个Model,它有一个实现INotifyPropertyChanged的相应ViewModel。

型号:

Public Class ItemModel

    Private _year As String

    Public Property Year As String
        Get
            Return _year
        End Get
        Set(value As String)
            _year = value
        End Set
    End Property

    Public Sub New()
        _year = Now.Year & "-" & (Now.Year + 1)
    End Sub

    Public Function ToString() As String
        Return _year & " Item Model"
    End Function
End Class

视图模型:

Imports System.ComponentModel

Public Class ItemViewModel
    Implements INotifyPropertyChanged

    Private _currentItem As ItemModel

    Public Property CurrentItem As ItemModel
        Get
            Return _currentItem
        End Get
        Set(value As ItemModel)
            If _currentItem IsNot value Then
                _currentItem = value
                NotifyPropertyChanged("CurrentItem")
            End If
        End Set
    End Property

    Public Sub New()
        _currentItem = New DciSurveyModel()
    End Sub

    Public Function ToString() As String
        Return _currentItem.Year & " Item ViewModel"
    End Function

    Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged

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

我将ItemsControl绑定到ViewModel的ObservableCollection,但ViewModel不会出现。我尝试使用ItemsTemplate创建一个文本框,设置Text = {Binding Path = CurrentItem.Year}无效。

XAML:

<Page x:Class="ItemPage"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      mc:Ignorable="d" 
      d:DesignHeight="300" d:DesignWidth="300"
      Name="ItemPage"
      Title="ItemPage" Loaded="Page_Loaded_1" Margin="10">

    <Grid>
        <ItemsControl ItemsSource="{Binding Path=ItemCollection}" />
    </Grid>
</Page>

以下是代码隐藏:

Imports OClient = Oracle.DataAccess.Client
Imports System.Collections.ObjectModel
Imports System.Data

Class ItemPage

    Private ds As DataSet
    Private itemsTable As DataTable
    Public Property ItemsCollection As ObservableCollection(Of ItemViewModel)

    Private Sub Page_Loaded_1(sender As Object, e As RoutedEventArgs)


        Dim itemsQry = "select item_year from items order by item_year desc"
        Dim queryCmd As New OClient.OracleCommand(itemsQry, O.con)
        Dim adapter As New OClient.OracleDataAdapter(queryCmd)
        ds = New DataSet
        adapter.Fill(ds, "items")
        itemsTable = ds.Tables("items")

        ItemsCollection = New ObservableCollection(Of ItemViewModel)

        For Each r As DataRow In surveys.Rows
            Dim newItem As New ItemViewModel
            newItem.CurrentItem.Year = r.Item("ITEM_YEAR").ToString
        Next

        Me.DataContext = Me
    End Sub
End Class

我很难搞清楚我的应用程序崩溃的地方。它是在我的ViewModels实现中吗?我没有正确绑定数据吗?我是否需要使用我的ObservableCollection做一些不同的事情?

感谢您帮助新手。

1 个答案:

答案 0 :(得分:2)

您遍历surveys.Rows的元素并为每个元素创建一个新的ItemViewModel,但您永远不会将它们添加到Items ItemsCollection.Add(newItem)的<:}>

For Each r As DataRow In surveys.Rows
    Dim newItem As New ItemViewModel
    newItem.CurrentItem.Year = r.Item("ITEM_YEAR").ToString

    ItemsCollection.Add(newItem) 
Next

您还使用了一个错误的Path来进行ItemsSource绑定。它必须是ItemsCollection而不是ItemCollection

除此之外,您应该声明一个DataTemplate而不是覆盖ToString()

<ItemsControl ItemsSource="{Binding ItemsCollection}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <TextBlock
                Text="{Binding CurrentItem.Year, StringFormat={}{0} Item ViewModel}"/>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>
相关问题