I have the following XAML in my WPF project:
<Window x:Class="DataBindToLocalVars.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TextBox x:Name="txt2" HorizontalAlignment="Left" Height="23" Margin="150,94,0,0" TextWrapping="Wrap" Text="{Binding Path=value1}" VerticalAlignment="Top" Width="120"/>
<TextBox x:Name="txt1" HorizontalAlignment="Left" Height="23" Margin="150,140,0,0" TextWrapping="Wrap" Text="{Binding Path=value2}" VerticalAlignment="Top" Width="120"/>
<Label Content="Value 2" HorizontalAlignment="Left" Margin="107,91,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.577,-0.602"/>
<Label Content="Value1" HorizontalAlignment="Left" Margin="107,145,0,0" VerticalAlignment="Top"/>
</Grid>
And the following code in my code-behind file:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
namespace DataBindToLocalVars
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public string value1
{
get { return value1; }
set
{
value1 = value;
this.NotifyPropertyChange("value1");
}
}
public string value2 {
get { return value2; }
set {
value2 = value;
this.NotifyPropertyChange("value2");
} }
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
value1 = "20";
value2 = "40";
}
public void NotifyPropertyChange(string propName)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
}
}
I am implementing the INotifyPropertyChange interface to be notified when the properties value1 and value2 change. But running this code gives me a stack overflow exception. Why is this?