When a property is changed via TwoWay binding, the variable is not operated, only with the dependency property.
Marked in the code comments the right places.
I know the workarounds (the most obvious - through PropertyChanged), but is it really that bad?
The question, in fact, is how easy it is to make two-way binding?
The project is here: https://yadi.sk/d/ioEe0PZ2v7pEC
MainWindow.xaml
<Window x:Class="SampleProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Sample" Height="100" Width="500"> <UniformGrid Columns="1" Margin="5"> <!-- Привязка на следующей строке НЕ вызовет setter переменной SliderValue --> <Slider Value="{Binding SliderValue, RelativeSource={RelativeSource AncestorType=Window}}"/> <TextBlock Text="{Binding TextValue, RelativeSource={RelativeSource AncestorType=Window}}"/> </UniformGrid> </Window>
MainWindow.xaml.cs
using System.Windows; namespace SampleProject { public partial class MainWindow { public MainWindow() { InitializeComponent(); SliderValue = 3.123456789; // <<== Это вызовет setter переменной SliderValue } // ==>> TextBlock.Text public string TextValue { get { return (string)GetValue(_propTextValue); } set { SetValue(_propTextValue, value); } } public static readonly DependencyProperty _propTextValue = DependencyProperty.Register("TextValue", typeof(string), typeof(MainWindow), new FrameworkPropertyMetadata("Ожидание значения...")); // ==>> Slider.Value public double SliderValue { get { return (double) GetValue(_propSliderValue); } set { TextValue = value.ToString(); SetValue(_propSliderValue, value); } } public static readonly DependencyProperty _propSliderValue = DependencyProperty.Register("SliderValue", typeof(double), typeof(MainWindow), new FrameworkPropertyMetadata(5.0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault)); } }