MVVM UpdateSourceTrigger

I am working on an MVVM application and have a view that is used to change a number of network parameters (IP, SubnetMask, etc.).

The view contains a number of text fields bound to properties in NetworkConfigViewModel:

<TextBox>
    <TextBox.Text>
        <Binding Path="IP" UpdateSourceTrigger="PropertyChanged"/>
    </TextBox.Text>
</TextBox>

... etc

The view also contains a button with a name Save Configuration. The button is bound to the RelayCommand in the ViewModel, which will take care of saving the configuration to the remote device upon request.

I would like to change the bindings of the text fields to use UpdateSourceTrigger="Explicit"so that the ViewModel is updated only when the user explicitly clicks “Save Configuration” and not updates as the values ​​change.

, BindingExpression.UpdateSource() . MVVM? RelayCommand ViewModel, , .

+3
3

, ViewModel View , . , . " ", , - ...

+1

. . , - , .

, , :

public string MyProperty
{
   get { return _Model.MyProperty; } 
   set
   {
      _Model.MyProperty=value;
      OnPropertyChanged("MyProperty");
   }
}

. " ".

+1

, :

ViewModel , , :

private string _myProperty;

public string MyProperty
{

    get { return _myProperty; }
    set
    {
        if (_myProperty != value)
        {
            _myProperty = value;
            OnPropertyChanged("MyProperty");
        }
    }
}

? , , - ? , TextBox ViewModel MyProperty, Model.MyProperty. _myProperty. Model.MyProperty, :

<TextBox Text="{Binding Model.MyProperty, UpdateSourceTrigger=Explicit}" />
<Button Command="{Binding SaveCommand}" />

... TextBox. , TextBox ""?

private void SaveExecute()
{
    // How do we update the Model.MyProperty value?
}
+1

Source: https://habr.com/ru/post/1733389/


All Articles