我正在使用MVVM Light框架构建SL4应用程序。我的简单应用主要由一个主视图(shellView)组成,该视图分为多个UserControl。它们只是UI的一个方便的分离,因此它们没有自己的ViewModel。Galasoft RelayCommand没有触发
ShellView包含一个包含多个KeypadButtons(自定义用户控件)的键盘(自定义用户控件)。
我很确定(因为我已经检查)DataContext设置正确,并且它被层次结构中的所有用户控件使用。 (ShellView的Datacontext是ShellViewModel,Keypad的DataContext是ShellViewModel等)。
在ShellViewModel中我有一个名为“ProcessKey”的ICommand(RelayCommand)。
在键盘控制,我有这样的:
<controls:KeypadButton x:Name="testBtn" Text="Hello">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding PressStandardKeyCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</controls:KeypadButton>
的KeypadButton基本上是包含一个按钮的网格。 MouseLeftButtonUp事件被捕获并且自定义的“Click”事件被触发。让我告诉你一些代码来轻松地解释我在做什么:
public partial class KeypadButton : UserControl
{
public delegate void KeypadButtonClickHandler(object sender, RoutedEventArgs e);
public event KeypadButtonClickHandler Click;
public KeypadButton()
{
// Required to initialize variables
InitializeComponent();
}
private void innerButton_Click(object sender, MouseButtonEventArgs e)
{
if (Click != null)
Click(sender, new KeypadButtonEventArgs());
}
}
public class KeypadButtonEventArgs : RoutedEventArgs
{
public string test { get; set; }
}
现在,如果我设置一个断点innerButton_Click的身体,我可以看到点击正确捕获,它包含指向RelayCommand。然而,没有任何反应:“点击(发件人,新的KeypadButtonEventArgs());”被执行,但仅此而已。
为什么这表现如此呢?不应该执行RelayCommand中定义的目标函数吗?也许是范围相关的问题?
在此先感谢, 干杯, Gianluca。
正如我在文章中所说,我正在使用SL4。据我所知,那里没有EventManager。我错了吗? – 2011-03-10 11:49:16
我试图将我的自定义事件处理程序声明为RoutedEventHandler,但仍然存在同样的问题。我可以看到Click事件中有一个EventTriggerBase实例,但实际上应该调用它时,什么都不会发生。任何帮助将非常感激!谢谢大家! – 2011-03-10 12:20:54
对不起,没有意识到你正在使用SL(下次需要更好地阅读)。根据这篇文章(http://www.silverlightshow.net/items/Routed-Events-in-Silverlight.aspx),SL中不支持自定义路由事件。 – 2011-03-10 18:51:07