【发布时间】:2014-10-21 06:31:11
【问题描述】:
我必须承认我无法理解 wpf 的工作方式。我的 Mainwindow 中嵌套了一个用户控件 BottomControl。如果单击底部控件中的按钮,我希望主窗口中发生某些更改(例如更改文本框的内容)。
很容易做的事情显然是在 Click_Event 中调用一个公共过程,但这不是很优雅。我已经使用了 RoutedCommands。
public static readonly RoutedCommand BottomGridReSize = new RoutedCommand();
在用户控件的 XAML 中
<Button Style="{StaticResource TransparentButton}" Command="{x:Static local:Commands.BottomGridReSize}" >
在主窗口的代码中
void BottomGridReSize_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
}
void BottomGridReSize_Executed(object sender, ExecutedRoutedEventArgs e)
{
\\Do some stuff
}
显然 Mainwindow 不能使用这些事件,因为 ist 无法识别它们。我错过了什么?
任何帮助将不胜感激
乔恩
【问题讨论】:
-
如果您使用 Command,请尝试将一些
CommandBinding添加到Window.CommandBindings集合中。
标签: c# wpf xaml command routedcommand