WPF:将用户控制命令路由到Mainwindow [英] WPF: Routing usercontrol command to Mainwindow

查看:613
本文介绍了WPF:将用户控制命令路由到Mainwindow的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须承认我在理解wpf的工作方式时遇到问题。我有一个用户控件 BottomControl 嵌套在我的主窗口中。如果在BottomControl中的一个按钮被点击,我想要在我的Mainwindow(例如改变文本框的内容)的某些更改发生。

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.

很容易做的是显然只是调用Click_Event中的公共过程,但这不是很优雅。我已经使用RoutedCommands。

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();

在用户控制的XAML中

In XAML of Usercontrol

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

在MainWindow代码中

In Code of MainWindow

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

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

很明显,Mainwindow不能使用这些事件,因为ist不能识别它们。

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

任何帮助将非常感激。

Jon

推荐答案

只是为了我的理解:你有一个BottomControl与一个按钮,当你点击按钮时,应该有一些发生在你的主窗口。所以为什么不在你的BottomControl中简单地创建类型ICommand的DependencyProperty并将其绑定到Button。

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

这篇关于WPF:将用户控制命令路由到Mainwindow的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆