WPF:将usercontrol命令路由到Mainwindow

时间:2014-10-21 06:31:12

标签: c# wpf xaml command routedcommand

我必须承认我在理解wpf的工作方式时遇到了问题。我在主窗口中嵌套了一个用户控件 BottomControl 。如果单击BottomControl中的Button,我希望在主窗口中进行某些更改(例如更改文本框的内容)。

很容易做的事情显然只是在Click_Event中调用一个公共过程,但这不是很优雅。我到目前为止使用RoutedCommands。

public static readonly RoutedCommand BottomGridReSize = new RoutedCommand();

在Usercontrol的XAML中

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

在MainWindow代码中

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

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

显然Mainwindow不能使用这些事件,因为ist无法识别它们。我错过了什么?

非常感谢任何帮助

乔恩

1 个答案:

答案 0 :(得分:1)

只是为了我的理解:你有一个带按钮的BottomControl,当你点击按钮时,你的主窗口会发生一些事情。那么为什么不在BottomControl中简单地创建一个类型为ICommand的DependencyProperty并将其绑定到Button。如果你这样做,你只需将MainViewmodel的命令绑定到此DP。

   <uc:BottomControl MyDPCommand="{Binding MyMainViewmodelCommand}" />
相关问题