WPF: Routing usercontrol command to Mainwindow

巧了我就是萌 提交于 2019-12-12 04:51:56

问题


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


回答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}" />


来源:https://stackoverflow.com/questions/26480279/wpf-routing-usercontrol-command-to-mainwindow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!