Pankaj Potdar Pankaj Potdar - 2 months ago 16
Vb.net Question

Why My MVVM does not work when I change data on the fly?

My xaml:

<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:TestMetroChartsVB"
xmlns:chart="clr-namespace:GravityApps.Mandelkow.MetroCharts;assembly=GravityApps.Mandelkow.MetroCharts"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<local:TestPageViewModel/>
</Window.DataContext>
<Grid>
<StackPanel>
<chart:ClusteredBarChart ChartTitle="Example1 " ChartSubTitle="Test1">
<chart:ClusteredBarChart.Series>
<chart:ChartSeries SeriesTitle="Errors" ItemsSource="{Binding Errors}"
DisplayMember="Category" ValueMember="Number"/>
</chart:ClusteredBarChart.Series>
</chart:ClusteredBarChart>
<Button x:Name="btnTest" Height="31" Margin="0,0,405,0" Content="Change Data"/>
</StackPanel>
</Grid>
</Window>


My view:

Imports GravityApps.Mandelkow.MetroCharts Class MainWindow

Private Sub btnTest_Click(sender As Object, e As RoutedEventArgs) Handles btnTest.Click
Dim test As New TestPageViewModel
test.changeData()
End Sub End Class


My viewModel:

Imports System.Collections.ObjectModel

Public Class TestPageViewModel

Public Property Errors() As ObservableCollection(Of TestClass)
Get
Return m_Errors
End Get
Private Set
m_Errors = Value
End Set
End Property
Private m_Errors As ObservableCollection(Of TestClass)
Public Sub New()
Errors = New ObservableCollection(Of TestClass)
Errors.Add(New TestClass("Data1", 5))
Errors.Add(New TestClass("Data2", 10))
Errors.Add(New TestClass("Data5", 15))
End Sub

Public Sub changeData()
Errors.Clear()
Errors.Add(New TestClass("DAta9", 10))
End Sub

End Class


My Model:

Public Class TestClass
Public Property Category As String
Get
Return m_Category
End Get
Set(value As String)
m_Category = value
End Set
End Property
Private m_Category As String
Public Property Number As Integer
Get
Return m_Number
End Get
Set(value As Integer)
m_Number = value
End Set
End Property
Private m_Number As Integer

Public Sub New(category As String, number As Integer)
Me.Category = category
Me.Number = number
End Sub End Class


It displays data initially like this:
enter image description here

But when I click my "change data" button data does not change?
What's the problem?

Answer

You are changing the wrong data:

Private Sub btnTest_Click(sender As Object, e As RoutedEventArgs) Handles btnTest.Click
    Dim test As New TestPageViewModel
    test.changeData()
End Sub

In your button's Click event handler, you create a new view model object and call the changeData() on it. But that's not the view model object the view is using, nor do you do anything to make it the view model object the view is using.

The best thing would be to just get the actual view model and change that one:

Private Sub btnTest_Click(sender As Object, e As RoutedEventArgs) Handles btnTest.Click
    Dim view As Button = CType(sender, Button)
    Dim test As TestPageViewModel = CType(view.DataContext, TestPageViewModel)
    test.changeData()
End Sub

The above assumes that the Button sending the Click event has inherited the MainWindow's data context.

Comments