0

I'm currently learning some basics in WPF and I've been looking for the mistake for about 2 days. Hope you guys can help.

I'm trying to update my UI (in this case the content of a label) by using INotifyPropertyChanged and a binding in XAML. The thing is: it only takes the first value and puts it in the content. Furthermore nothing happens but the event (OnPropertyChanged) is fired.

This is what I have in XAML:

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication1" x:Class="MainWindow"
    Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <local:View x:Key="ViewModel"/>
    </Window.Resources>

    <Grid Margin="0,0,2,-4" DataContext="{Binding Source={StaticResource ViewModel}}">
....
    <Label x:Name="lbl_money" Grid.ColumnSpan="2" Content="{Binding Path=PropMoney}" HorizontalAlignment="Left" Margin="403,42,0,0" VerticalAlignment="Top">

And this is the necessary part of my class View:

Public Class View
Inherits ViewModelBase

Private rest1 As New Restaurant
Private mainPlayer As New Player
Private mycurrentMoney As Double = 3
Private currentClickIncrease = mainPlayer.PropClickIncrease

 Public Property PropMoney() As Double
    Get
        Return mycurrentMoney
    End Get
    Set(value As Double)
        mycurrentMoney = value
        OnPropertyChanged("mycurrentMoney")
    End Set
End Property

Sub SelfClicked()
    PropMoney() += 1
End Sub

Last but not least the MainWindow class, where i instantiate my view:

Class MainWindow

Private view As New View

    Sub New()
        InitializeComponent()
    End Sub

    Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
        view.SelfClicked()
    End Sub

End Class

So my mycurrentMoney is increasing each click and the event is fired but the label doesn't update.

Thank you in advance!

Charles
  • 17
  • 5

3 Answers3

2

Your OnPropertyChanged("mycurrentMoney") statement won't raise a property change on your property, because it's called PropMoney.

You have to set OnPropertyChanged("PropMoney") in your setter instead.

2

If you have Visual Studio 15 use NameOf operator instead of string literal like so:

NameOf(PropMoney);

If you later rename your property, it will still work opposed to string literal which will NOT. Alternatively modify your OnPropertyChange to make use of CallerMemberName

OnPropertyChange ([System.Runtime.CompilerServices.CallerMemberName] string memberName = "")
{

}

The property name will be filled in, this works only in setter for current property however.

Also, set DataContext for whole window (Setting DataContext in XAML in WPF). DataContext={StaticResource ViewModel} and don't use Path in your Binding, just {Binding PropertyName}

Community
  • 1
  • 1
Filip
  • 1,824
  • 4
  • 18
  • 37
0

There are 2 problems with your code

First you raise PropertyChanged event for the backing field and should raise it for property name

OnPropertyChanged("PropMoney")

Second the property you change belong to different instance of View then the one set as DataContext. So in XAML remove DataContext changes, only leave property binding

<Window ...>
    <Grid Margin="0,0,2,-4">
        <!-- .... -->
        <Label ... Content="{Binding Path=PropMoney}">

and then in code set DataContext of MainWindow to the instance that you create and modify

Class MainWindow

Private view As New View

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

    Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
        view.SelfClicked()
    End Sub

End Class
dkozl
  • 32,814
  • 8
  • 87
  • 89