1

I'm trying to create a WPF Application using data bindings. I've done it like it is showed here but my labels aren't updating the value when it's changed. I think the reason for that is, that PropertyChanged equals null

Here is my XAML:

<Window x:Name="MainWindow1" x:Class="Gui.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:Gui"
    mc:Ignorable="d"
    Title="MainWindow" Height="315.448" Width="1131.79" ResizeMode="NoResize" Background="#FFFDF9F9">

    <Grid Margin="0,0,2,0">
        <Label x:Name="stopWatchMethod1" Content="{Binding Path=TimeMethod1, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" Margin="343,69,0,0" VerticalAlignment="Top" Height="28" Width="440"/>
    </Grid>

</Window>

and my Code behind looks like this:

public partial class MainWindow : Window, INotifyPropertyChanged
{
    public MainWindow()
    {
        InitializeComponent();
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private string timeMethod1 = "---";

    public string TimeMethod1
    {
        get { return timeMethod1; }
        set
        {
            timeMethod1 = value;

            NotifyPropertyChanged();
        }
    }


    protected virtual void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        var handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

I'm setting the value right here:

ts = stopWatch.Elapsed;
elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10);
TimeMethod1 = elapsedTime;
Community
  • 1
  • 1
RohrerF
  • 33
  • 1
  • 10

2 Answers2

2

You didn't set the DataContext.

In your constructor write:

public MainWindow()
{
    InitializeComponent();
    DataContext = this;
}

this enables your controls to listen for property changed events triggered by your MainWindow (the DataContext)

Tommaso Bertoni
  • 2,333
  • 1
  • 22
  • 22
-1

There was some error with your code. I have corrected it. The property name was not defined. Please check with below code. It should work.

public string TimeMethod1
{
        get { return timeMethod1; }
        set
        {
            timeMethod1 = value;

            NotifyPropertyChanged("TimeMethod1");
        }
}
ViVi
  • 4,339
  • 8
  • 29
  • 52
  • It's still not working. In the method _NotifyPropertyChanged_ var handler is null ==> it isn't doing anything – RohrerF May 03 '16 at 08:34
  • Then you might not be setting the datacontext correctly. Inside your view's constructor set the datacontext to your viewmodel class instance. – ViVi May 03 '16 at 08:43
  • Well, my constructor is in the code above. All the other constructors in my code aren't doing something with my UI – RohrerF May 03 '16 at 08:47
  • This is not an error. Take a look at the [CallerMemberNameAttribute \[CallerMemberName\]](https://msdn.microsoft.com/library/system.runtime.compilerservices.callermembernameattribute(v=vs.110).aspx) – Tommaso Bertoni May 03 '16 at 08:48
  • 1
    this.DataContext = this; Do this in your constructor. – ViVi May 03 '16 at 08:49
  • Also could you try the same with a TextBlock. Set the binding to TextBlock's Text property and chekc if it's working. – ViVi May 03 '16 at 08:50