0

I must confess I have problems understanding the way wpf works. I have a usercontrol BottomControl nested in my Mainwindow. If a Button in BottomControl is clicked I want certain changes in my Mainwindow (changing content of a textbox for example) to occur.

The easy thing to do is obviously to just call a public procedure in the Click_Event but this is not quite elegant. I got so far as to use RoutedCommands.

public static readonly RoutedCommand BottomGridReSize = new RoutedCommand();

In XAML of Usercontrol

<Button Style="{StaticResource TransparentButton}" Command="{x:Static local:Commands.BottomGridReSize}" >

In Code of MainWindow

        void BottomGridReSize_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
        }

    void BottomGridReSize_Executed(object sender, ExecutedRoutedEventArgs e)
       {
          \\Do some stuff
       }

Obviously Mainwindow can't use these events because ist doesn't recognize them. What am I missing?

Any help would very much be appreciated

Jon

JonBlumfeld
  • 299
  • 2
  • 5
  • 20

1 Answers1

1

just for my understanding: you have a BottomControl with a Button and you when the Button is clicked there should something happen in your mainwindow. so why not simply create a DependencyProperty of type ICommand in your BottomControl and bind this to the Button. if you do this you can simply bind a Command of your MainViewmodel to this DP.

   <uc:BottomControl MyDPCommand="{Binding MyMainViewmodelCommand}" />
blindmeis
  • 22,175
  • 7
  • 55
  • 74
  • Hi John, have you thought about implementing the MVVM pattern instead of going the code- behind route? This way you can implement the ICommand interface and have all your command methods in a view model. – Hardgraf Oct 21 '14 at 10:35
  • @Hardgraf: I'm well over my head with WPF as it is. I tried MVVM first but gave it up quite quickly. – JonBlumfeld Oct 21 '14 at 19:46
  • @blindmeis: this seems to be the way to go. I'll try it tomorrow. – JonBlumfeld Oct 21 '14 at 19:47
  • @JonBlumfield, take a look at my blog: http://wpffromscratch.wordpress.com/2014/07/10/simple-mvvm-with-entity-framework-part-1-prepping-the-project/ I wrote a complete beginners MVVM walkthrough which takes you step by step through a simple project. I also learnt on omy own and really struggled to find a basic example hence writing this. Cheers – Hardgraf Oct 22 '14 at 11:15