1

I'm hoping someone out there can help me. I'm trying to convert a legacy winforms app to WPF using MVVM. I've broken up the main window of the application into 4 main UserControls. The UserControls display different types of data objects and each UserControl has it's own ViewModel. Some of the data objects are inter-changeable between the different UserControls, for instance 'User Control 1' can contain strings objects and so can 'User Control 2 and 'User Control 3' (see diagram below).

enter image description here

My question is how can I handle the Cut, Copy, Paste commands in the toolbar? To possibly make things more complicated, each UserControl can contain a selected object at the same time as the other UserControls contain a selected object and User Control 2 is a WindowsFormsHost wrapper around a winforms control.

So far I've tried using ApplicationCommands but I can't even get them to fire. I've pasted a snippet of the code I thought would work using the ApplicationCommands below. Any help with this would be really appreciated.

<Button Command="ApplicationCommands.Cut" />

and on the UserControls

  <UserControl.CommandBindings>
    <CommandBinding Command="ApplicationCommands.Cut" Executed="executed_Cut" CanExecute="canExecute_Cut" />
  </UserControl.CommandBindings>

and finally in the UserControl's code behind (I know this isn't great)

    public void executed_Cut(Object sender, ExecutedRoutedEventArgs e)
    {
        //execute code here
    }

    public void canExecute_Cut(Object sender, CanExecuteRoutedEventArgs e)
    {
        //can execute code here  
    }
fedorqui
  • 275,237
  • 103
  • 548
  • 598
justdruit
  • 45
  • 7
  • First thing you need is to enable the buttons with something like this in the canExecute_Cut. e.CanExecute = true; //or some other condition in your app. – strattonn Feb 12 '14 at 01:47
  • Hi strattonn, sorry I should have said; I set e.CanExecute equals true or false in the canExecute_Cut method base on my code requirements. I just omitted it here because at the moment executed_Cut and canExecute_Cut are never get called. – justdruit Feb 12 '14 at 12:07

1 Answers1

0

I successfully use the behavior approach described in this question to avoid putting any code in my view model. Then whenever focus goes to a control which has a copy/paste behavior defined the toolbar cut/copy/paste buttons "light up" accordingly.

Community
  • 1
  • 1
Darren Gosbell
  • 1,941
  • 13
  • 17