Legends
Legends

Reputation: 22662

Disable multiple GUI controls on button click

I bind DelegateCommands to buttons in the UI. And I am not using Prism. Now I want to disable/hide certain controls on the UI when a button is clicked.

Do I have to put the Disable/Hide logic into the execute handler of the DelegateCommand itself? Special focus here: background Worker thread.

  this.MyCommand = new DelegateCommand(MyExecutehandler);

    void MyExecutehandler(object obj){
    // 1.) disable controls here
    // 2.) long running operation on background worker here
    // 3.) enable the controls again in the worker_completed handler?
    }

The controls would then be disabled/enabled through MVVM.

Upvotes: 0

Views: 149

Answers (2)

pengMiao
pengMiao

Reputation: 334

Special focus here: background Worker thread

So i just make an assumption that you are asking how to modify UI control state from another thread.

Normally we use way below to modify UI thread's control from worker thread

this.Dispatcher.BeginInvoke( <your delegate here>)

But i suspect that you can't do that in background worker thread as 'this' will mostly differ. So you may try this :

button.Dispatcher.BeginInvoke(new Action(()=>
{
 //disable button here
}));

It is like worker thread send a message to tell UI thread to run the delegate.

One more thing, normally we disabled a control and re-enable a button after some process to avoid some unnecessary attached event/propertychanged event. So I suspect you may need this:

You can done that by removing event temporarily:

 button -= button_click_event 

and Re-add event after that

 button += button_click_event

Hope this helps.

Upvotes: 1

Pratik Parikh
Pratik Parikh

Reputation: 158

You can bind visibility property of controls which you want to disable and in MyExecutehandler function set property values to visibility.collapse

Upvotes: 0

Related Questions