l33t
l33t

Reputation: 19976

Exactly when are bindings updated?

When a DependencyProperty is set, does the bound target (viewmodel) property get its new value immediately or does this happen asynchronously? E.g.:

<TextBox x:Name="textBox" IsEnabled="{Binding IsEnabled,Mode=TwoWay}"/>

Then I explicitly set the IsEnabled property:

textBox.IsEnabled = false;

I know about the UpdateSourceTrigger setting, and I believe this question is mostly relevant for the default UpdateSourceTrigger.PropertyChanged value.

Upvotes: 1

Views: 53

Answers (2)

Michał Komorowski
Michał Komorowski

Reputation: 6238

The short answer is immediately and synchronously.

The long story

I would not mix UpdateSourceTrigger with (a)synchronicity of the binding. The first defines how often the source property should be updated e.g. when a control loses focus or maybe when the UpdateSource method is called...

On the another hand the synchronicity of a given binding defines if get/set operations of a source property are performed synchronously or asynchronously. In practice you will only see a difference if reading a source property might take a long time. In that case by default UI will be blocked.

You can control the synchronicity of the binding via Binding.IsAsync property. Here is an example showing a difference. Let's start with XAML:

<TextBox Text="{Binding Text, Mode=TwoWay}"/>
<Button Click="Button_Click">Update</Button>

And here is a code behind. MainWindow has Text property which is a source of a binding. If a button is clicked it is updated. If you examine Text property you will see that it takes 10 seconds to get its value.

public partial class MainWindow : Window, INotifyPropertyChanged
{
    private string _text;
    public string Text
    {
        get
        {
            System.Threading.Thread.Sleep(10000);
            return _text;
        }

        set
        {
            _text = value;
            OnPropertyChanged(nameof(Text));
        }
    }

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

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        Text = DateTime.Now.ToString();
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

If you run this code you will observe 2 things:

  • The application starts slowly.
  • If you click a button, the application hangs.

Now modify binding in this way:

<TextBox Text="{Binding Text,Mode=TwoWay, IsAsync=true}"/>

Run the application once again:

  • The application starts immediately.
  • If you click a button, the application doesn't hangs.

The similar excerise can be performed with a settter of Text property i.e. just move System.Threading.Thread.Sleep(10000); to the setter.

Upvotes: 2

mahendramaid
mahendramaid

Reputation: 25

you can give getter setter properties to textbox field and find how this working like ..string st get { return _type; } set { _type = value; }..and so you will get chance to chnage values or you can apply validation

Upvotes: 1

Related Questions