Home > Mobile >  Enable Save button when textchange/selected item change in user control
Enable Save button when textchange/selected item change in user control

Time:12-29

<TextBox Grid.Row="1" Grid.Column="2" Width="50" Text="{Binding Size}"></TextBox>
<ComboBox Grid.Row="2" Grid.Column="1" ItemsSource="{Binding MyList, Mode=OneTime}" 
                  SelectedValue ="{Binding ListSelectedItem, Mode=TwoWay}"></ComboBox>
<Button Grid.Row="3" Grid.Column="2" Content="Save" Command="{Binding SaveCommand}" IsEnabled="False"/>

ViewModel,

public ICommand SaveCommand { get; }
public ConfigurationViewModel()
{
     SaveCommand = new RelayCommand(SaveCommandExecute);
}
public int Size
    {
        get
        {
            return _size;
        }

        set
        {
            _size = value;
            OnPropertyChanged();
        }
    }

    private void SaveCommandExecute()
    {
        // save logic
    }

I can able to save the data entered in textbox and selected value in combo box. By default, Save button should be disabled and if any change in textbox / combobox then enable the Save button.

if user reverts back to old value in textbox / combobox then Save button should be disabled back.

CodePudding user response:

In yours RelayCommand might be a second parameter of Func<object, bool> canExecute. Then you can add method for checking if value has changed.

private bool CanSaveCommandExecute(object parameter) => this.Size != defaultValue;

And in your constructor add name of that method in SaveCommand as second parameter. I hope this will help you.

  • Related