1
votes

I have a very simple piece of code to understand the behavior that what happens when we have assigned a Binding expression to any dependency property and then assign a direct value to that dependency property. Following is code

View XAML

<StackPanel>
    <Button Click="Button_Click" Content="Assign binding value" />
    <Button Click="Button_Click_1" Content="Assign direct value" />
    <TextBox Text="{Binding TextSource, Mode=OneWay}" x:Name="stf" />
</StackPanel>

View XAML.cs

public partial class MainWindow : Window
{
    MainViewViewModel vm = new MainViewViewModel();
    public MainWindow()
    {
        InitializeComponent();

        DataContext = vm;
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        vm.TextSource = "Value set using binding";
    }

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        stf.Text = "New direct value";
    }
}

ViewModel

public class MainViewViewModel : INotifyPropertyChanged
{
    //INotifypropertychanged implementation here ...

    private string _textSource;

    public string TextSource
    {
        get { return _textSource; }
        set
        {
            _textSource = value;
            OnPropertyChanged("TextSource");
        }
    }

}

Now my observation is

  1. When I click "Assign binding value" the view is updated with binding source value. (As expected)
  2. When I click "Assign direct value" the view is updated with the direct value assigned to text field (As expected)
  3. I assume that at this stage the binding is broken and when I click on "Assign binding value" again it should not work, mean no UI update. and it works according to my expectations (As expected)
  4. The confusing point is when I set the binding mode to "TwoWay", then point 3 is not happening, and it always keeps working whatever button I press. both from binding source and direct value. (Not clear to me what TwoWay binding need to do with this)

Anyone please clarify it?

2

2 Answers

2
votes

I think that TwoWay binding will assign the value of the property in the View Model to the dependency property of the UI element, and also if you change at any point the value of the dependency property (like when you press the button "Assign direct value") then the new value of the dependency property will also be assigned to the property in the View Model.

In other words I think TowWay mode actually means that the value can be assigned from the View Model to the UI and from the UI to the View Model.

2
votes

While setting DependencyProperty value, DependencyObject checks if the new value is the BindingExpression or not. If not it checks if the previous value was the binding expression. For previous binding expression it tries to set the value for the expression. BindingExpression while trying to set the value checks the mode, and for OneWay it returns false without setting the value and deactivates and detaches the binding expression for the dependency property. Hence for Mode=OnWay, binding gets deactivated and detached for a dependency property.

For TwoWay, since BindingExpression is able to set the value, hence binding is not deactivated and continues to work.