wpfmvvminotifypropertychangedbindable

Update the target from the source with bindable base WPF


This is bindable property in viewmodel

    private string _tooltip;
    public string Tooltip
    {
        get { return _tooltip; }
        set
        {
            _tooltip = value;
            SetProperty(ref _tooltip, value);
        }
    }

xaml

<TextBox HorizontalAlignment="Stretch"
                             Margin="2"
                             Text="{Binding  Path=Tooltip, Mode=TwoWay}"
                             MinWidth="40"
                             Height="24" />

When this tooltip is changed in the viewmodel, view is not updated. How to update the view from source to target?


Solution

  • From the online documentation of BindableBase.SetProperty:

    Checks if a property already matches a desired value. Sets the property and notifies listeners only when necessary.

    So you must not call _tooltip = value before SetProperty, because if you do, SetProperty will not fire the PropertyChanged event:

    private string _tooltip;
    public string Tooltip
    {
        get { return _tooltip; }
        set { SetProperty(ref _tooltip, value); }
    }