toni
toni

Reputation: 1

WPF Enable/Disabled controls from a page placed inside main window application

I have an WPF application that has a main window. In the left side of this Window there are some buttons into a listbox, it is a kind of menu to access faster to pages. These buttons belongs to pages that they are loaded inside the window when the user selects one.

Main window also has another main menu in the top for doing other tasks.

When a page is loaded in the main window and the user clicks a button of this currently loaded page, it starts a task that takes a long time. While this long task is executing I want the user can not select (or press) any of the buttons into the listbox because In the loaded page the long task also is updating the UI for this page. I would like to disabled (isEnabled=false) the listbox when long task is executing and not to enabled it until the long task has finished. How can I do this? I mean, from the page is currently loaded I want to disabled the listbox placed in the main window that is the owner.

The listbox doesn't belong to the currently loaded page.

Thanks!

Upvotes: 0

Views: 1242

Answers (2)

user304602
user304602

Reputation: 991

Finally I have learnt about how to do with Icommand. After spending a lot of time, I saw what powerful it was and I bound all my clickable controls in what I was interested to re-enabled/disabled to commands and the commands with canBeExecute return false when long tasks (background workers) is in progress. I have implemented a command static class to check if one of the background workers (long tasks) is running and into it one method that return true or false depending on whether one task is running. So in canExecuted method I check this accessing the method placed into the class.

Thanks for your good idea!

Upvotes: 0

Steve Psaltis
Steve Psaltis

Reputation: 665

The 'proper' way to do this is to implement a custom command (ICommand) for your button. You can then set the Command property on your button to be your custom command. While the command is executing i.e. in the Execute method you can set an _isRunning field to true and then in your CanExecute implemetation you can return a value based on _isRunning : e.g ...

public class YourButtonCommand : ICommand {

...

public void Execute
{
    _isRunning = true
     ... do your window loading here
    _isRunning = false;
}

public bool CanExecute(object parameter)
{
    return !_isRunning;
}

....

}

WPF will take care of disabling the button if your command is implemented right or you can implement a DataTrigger on your control to dissable it based on the state of the command. For information about how your command code should be implemented please check out Josh Smiths MVVM article

Upvotes: 1

Related Questions