无法在XAML中为此代码声明viewmodel

时间:2017-05-03 04:13:40

标签: wpf vb.net xaml mvvm

我已经审查过很多'如何在XAML中声明视图模型'帖子我可以而且仍然无法解决这个问题。我正在使用下面的简单程序来学习有关绑定的基础知识,这段代码可以正常工作。当我单击以插入(添加)项目时,列表框会自动反映该更改,以及清除列表时。

在代码之后查看我的问题。

模型

Namespace MVVM3
  Public Class MyListItem
    Public Property MyListItemID() As Integer
    Public Property Name() As String
  End Class
End Namespace

视图模型

Namespace MVVM3
  Public Class ViewModel
    Implements INotifyPropertyChanged

    Public Property allIdeas() As New ObservableCollection(Of MyListItem)

    Public Sub New()
      For index = 0 To 9
        Dim anItem As New MyListItem
        anItem.Name = "Idea " & index
        allIdeas.Add(anItem)
      Next
    End Sub

    Public Sub InsertAnItem()
      Dim anItem As New MyListItem
      anItem.Name = "Item " & allIdeas.Count()
      allIdeas.Add(anItem)
      NotifyPropertyChanged("allIdeas")
    End Sub

    Public Sub ClearStoredList()
      allIdeas.Clear()
      NotifyPropertyChanged("allIdeas")
    End Sub

    Public Event PropertyChanged As PropertyChangedEventHandler _
        Implements INotifyPropertyChanged.PropertyChanged

    Public Sub NotifyPropertyChanged(ByVal propertyName As String)
      RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
    End Sub
  End Class

End Namespace

查看

<Window x:Class="MVVM3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:mvvm3"
        Title="MainWindow"  SizeToContent="WidthAndHeight" WindowStartupLocation="CenterScreen"
        >

  <StackPanel Grid.Column="1">
    <Button Margin="25" Content="Insert an item" Click="InsertAnItem_Click"/>
    <Button Margin="25" Content="Clear stored list" Click="ClearStoredList_Click"/>
    <ListBox Name="listBox3" ItemsSource="{Binding allIdeas}" DisplayMemberPath="Name" Height="100">
    </ListBox>
  </StackPanel>
</Window>

背后的代码

Namespace MVVM3
  Partial Class MainWindow
    Private vm = New ViewModel

    Sub New()
      InitializeComponent()
      DataContext = vm
    End Sub

    Private Sub InsertAnItem_Click(sender As Object, e As RoutedEventArgs)
      vm.InsertAnItem()
    End Sub

    Private Sub ClearStoredList_Click(sender As Object, e As RoutedEventArgs)
      vm.ClearStoredList()
    End Sub
  End Class
End Namespace

我想将ViewModel声明移到XAML中以消除后面的代码。如果我发表评论DataContext = vm,无论我在各个帖子中采用何种方法,绑定都不会更新列表框。

以下更改会导致列表框显示在ViewModel.New中发生的初始分配,但之后不会反映任何更改:

<Window x:Class="MVVM3.MainWindow"
  ...
  xmlns:local="clr-namespace:mvvm3"
  xmlns:vm="clr-namespace:mvvm3.MVVM3"
  ...
  >

  <Window.DataContext>
    <vm:ViewModel/>
  </Window.DataContext>

我错过了什么?这是命名空间问题吗?

我希望继续使用Commands和ViewModel定位器,但在我理解之前我不知道如何做到这一点。

1 个答案:

答案 0 :(得分:0)

在XAML中声明ViewModel类的实例时,可以通过将DataContext属性的值转换为ViewModel类型来在代码后面访问它:

Partial Class MainWindow
    Sub New()
       InitializeComponent()
    End Sub

    Private Sub InsertAnItem_Click(sender As Object, e As RoutedEventArgs)
        CType(DataContext, ViewModel).InsertAnItem()
    End Sub

    Private Sub ClearStoredList_Click(sender As Object, e As RoutedEventArgs)
        CType(DataContext, ViewModel).ClearStoredList()
    End Sub
End Class