-6

Here I have added a boolean property in model and binding it to Isenabled property of Combobox . And Binding Command to button in which I have added execute() method which sets the property binded to combobox. However this changes isEnable of Button. Is there a way to disable combobox on btn click?

Model:

    public class MyPanelModel : BindableBase
      {
    private  bool _MessageVisibilty;
    public bool MessageVisibilty
    {
        get
        {
            return _MessageVisibilty;
        }
        set
        {
            SetProperty(ref _MessageVisibilty, value);
        }
    }
}

View Model:

    private MyPanelModel plModel;
    public MyPanelModel MyPnlModel
    {
        get { return plModel; }
        set { SetProperty(ref plModel,value); }

    }           

    private readonly DelegateCommand _hideCommand;

    public DelegateCommand DisableOtherControlsCommand
    {
        get { return _hideCommand; }
    }

    public MyPanelViewModel()
    {
    _hideCommand = new DelegateCommand(hideExecute, canExecute);
    _hideCommand.RaiseCanExecuteChanged();
    }

    private void hideExecute()
    {
        MyPnlModel.MessageVisibilty = false;
    }

    private bool canExecute()
    {

            return true;

    }

Xaml :

    <ComboBox  Mandatory="True">
 <i:Interaction.Triggers >
   <i:EventTrigger EventName="IsEnabled">
     <prism:InvokeCommandAction Command="{Binding MyPanelModel.MessageVisibilty,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"  />
       </i:EventTrigger>
          </i:Interaction.Triggers>
           </ComboBox>

    <Button Margin="10" Command="{Binding MyPanelViewModel.DisableOtherControlsCommand}" >Save</Button>
G Gk
  • 1
  • 1
  • 1
    What have you tried so far (show us your code)? – Skandix Feb 16 '18 at 08:57
  • 2
    The basic idea is to create a command in your ViewModel that sets one of its properties to false. Then bind the "IsEnabled" property in your view to this property. – Kilazur Feb 16 '18 at 08:58

1 Answers1

0

If you want to use MVVM the idea is to bind the IsEnable property of your button to a boolean in your viewmodel.

If your viewmodel implements INotifyPropertyChanged and is set as a datacontext for your view, when you will update the boolean value in your viewmodel, the control which is bind to the boolean property should be enable/disabled

If you wand to change the boolean after a click on the button you can simply create a Command and bind your button Command property on it. In your command method implementation, set the boolean value to true/false it will reflect on the ui's isenable property immediately

For Command creation, here is another topic that may help you : How to bind WPF button to a command in ViewModelBase?