Click here to Skip to main content
15,881,877 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi Guys,

I'm back again with another question, however on this one I've not actually tried anything yet. This is because I don't really have a clue where to start.

Scenario
I have an WPF application built on MVVM principles (although it is not 100% MVVM I think).
The application has the following structure:

Main Window
    ->ViewContainer (has a menu system in it that creates buttons based on viewmodel)
         -> View A
         -> View B
         -> View C


Problem/requirement
View C has a button on it that calls a command on its underlying viewmodel, now the issue is this is a long running command and what I want to do is prevent the user from doing any user interaction but keep the application able to update. The process runs on a separate thread so the UI can update an onscreen log. But the user can still click buttons in the ViewContainer menu and change views. I need to prevent this because the other options shouldn't run while this process is running.

Currently I am able to disable interaction View C, but with trying to adhear to MVVM I'm not sure how I can apply this behavior to parent containers?

Any help/pointers would be appreciated.

Need more info, just ask.
Posted
Comments
Marvin Ma 5-Sep-13 5:52am    
So, you want the buttons to be disabled when the application updates?
I hope i got it correctly?
Pheonyx 5-Sep-13 6:13am    
Basically yes that is correct, I can make that work on the view that is bound to the view model, but I need that to bubble up to the parent to prevent the user from changing from View C to views A / B

1 solution

Try to define a command for the buttons which are responsible for the change of the view.
You should use a Delegate Command for this.

C#
class DelegateCommand : ICommand
    {
        private readonly Predicate<object> _canExecute;
        private readonly Action<object> _execute;

        public event EventHandler CanExecuteChanged;

        public DelegateCommand(Action<object> execute)
        {
            _execute = execute;
        }

        public DelegateCommand(Action<object> execute, Predicate<object> canExecute)
        {
            _execute = execute;
            _canExecute = canExecute;
        }



        public bool CanExecute(object parameter)
        {
            if (_canExecute == null)
            {
                return true;
            }

            return _canExecute(parameter);
        }

        public void Execute(object parameter)
        {
            _execute(parameter);
        }

        public void RaiseCanExecuteChanged()
        {
            if (CanExecuteChanged != null)
            {
                CanExecuteChanged(this, EventArgs.Empty);
            }
        }
    }


It gives you the opportunity to define whether the command is executable or not.

To do this, try doing something like this:


C#
//
public ICommand SwitchViewCommand { get; set; }
        public void SwitchView(object parameter)
        {
            //Your Logic here.
        }


C#
public bool AllowSwitchView(object parameter)
        {
            //Your Conditions here
        }


And dont forget to instantiate your command:

C#
SwitchViewCommand = new DelegateCommand(SwitchView, AllowSwitchView);


I hope this will help you :)

And sorry for my bad english.
 
Share this answer
 
v2
Comments
Pheonyx 6-Sep-13 9:04am    
Just wanted to say thanks, although I couldn't get your approach working it did give me an idea on how I could modify my existing code to do something similar. Kudos will go to you for providing the inspiration, thank you :)
Marvin Ma 6-Sep-13 9:13am    
Good to hear, that you found a solution :)
And thank you for your kindly words.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900