2017-12-12 14 views
1

내 데이터베이스의 항목 목록으로 페이지를 채우려고합니다. 이 항목에 대한 모델과 뷰 모델을 만들었지 만 ItemsControl에 나타나지 않습니다.ItemsControl이 내 ViewModels를 표시하지 않는 이유는 무엇입니까?

저는 모델에 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 

내가 ViewModels의 ObservableCollection에에 ItemsControl에 바인딩하지만 ViewModels가 표시되지 않습니다. 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과 다른 무언가를해야합니까?

초보자를 도와 주셔서 감사합니다.

답변

2

당신은 surveys.Rows의 요소를 반복하고 각각에 대해 새로운 ItemViewModel을 만들 수 있지만 당신은 ItemsCollection.Add(newItem)에 의해, ItemsCollection에 추가하지 :

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 

을 당신은 또한 ItemsSource 바인딩에 대한 잘못된 경로를 사용하는 . ItemCollection 대신 ItemsCollection이어야합니다.

<ItemsControl ItemsSource="{Binding ItemsCollection}"> 
    <ItemsControl.ItemTemplate> 
     <DataTemplate> 
      <TextBlock 
       Text="{Binding CurrentItem.Year, StringFormat={}{0} Item ViewModel}"/> 
     </DataTemplate> 
    </ItemsControl.ItemTemplate> 
</ItemsControl> 
+1

감사합니다 : 당신이 DataTemplate을 선언한다 대신 최우선 ToString()의 게다가

! 나는 그들을 내 컬렉션에 추가하는 것을 잊어 버렸고 게시 된 코드의 오타에 약간 당혹 스러웠다. ItemsControl 템플릿을 보는 것은 매우 유용합니다. 고맙습니다! – acdn