1

I have a simple WPF window with a slider and two textblocks. As the slider moves it updates a data bound object. Now the first textblock updates while the second does not. Why?

You may say there is no INotifyPropertyChanged here. But then why is the first updating? I have pulled my hair enough. Please help.

My WPF app in all its glory is as follows.

<Window x:Class="DataTriggerDemo.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:DataTriggerDemo"
            mc:Ignorable="d"
            Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>

        <Slider x:Name="MySlider" Margin="5" Minimum="0" Maximum="100"
                    Value="{Binding TheValue}"/>
        <TextBlock Grid.Row="1" Text="{Binding TheValue}" />
        <TextBlock Grid.Row="2" Text="{Binding TheValueTwice}" />
    </Grid>
</Window>

And now the code behind.

using System.Windows;
namespace DataTriggerDemo
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = new DataObject();
        }
    }

    public class DataObject
    {
        private int _theValue;
        public int TheValue
        {
            get { return _theValue; }
            set {
                _theValue = value;
                TheValueTwice = _theValue * 2;
            }
        }
        private int _theValueTwice;
        public int TheValueTwice
        {
            get {
                return _theValueTwice;
            }
            set {
                _theValueTwice = value;
            }
        }
    }
}
VivekDev
  • 20,868
  • 27
  • 132
  • 202

1 Answers1

1

Actually you are encountering a another hidden aspect of WPF, that's it WPF's data binding engine will data bind to PropertyDescriptor instance which wraps the source property if the source object is a plain CLR object and doesn't implement INotifyPropertyChanged interface. And the data binding engine will try to subscribe to the property changed event through PropertyDescriptor.AddValueChanged() method. And when the target data bound element change the property values, data binding engine will call PropertyDescriptor.SetValue() method to transfer the changed value back to the source property, and it will simultaneously raise ValueChanged event to notify other subscribers (in this instance, the other subscribers will be the TextBlocks within the ListBox.

Please refer to: https://social.msdn.microsoft.com/Forums/vstudio/en-US/9365bb6a-b411-4967-9a03-ae2a810fb215/data-binding-without-inotifypropertychanged?forum=wpf

Iron
  • 926
  • 1
  • 5
  • 16