2017-12-12 11 views
1

データベースからアイテムのリストをページに挿入しようとしています。これらのアイテムのモデルとビューモデルを作成しましたが、ItemsControlには表示されません。ItemsControlが私のViewModelsを表示しないのはなぜですか?

私は、対応するViewModelを持ち、INotifyPropertyChangedを実装しているモデルを持っています。

モデル:

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 

のViewModel:

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の代わりに​​でなければなりません。

それに加えて、代わりにToString()をオーバーライドするあなたはDataTemplateを宣言する必要があります

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

ありがとうございました!私は自分のコレクションに追加するのを忘れて、私のコードの誤植を忘れて、ちょっと恥ずかしいです。 ItemsControlテンプレートを見ると非常に便利です。ありがとうございました! – acdn

関連する問題