Galasoft RelayCommand 未触发
我正在使用 MVVM Light 框架构建 SL4 应用程序。我的简单应用程序主要由一个主视图(shellView)组成,它分为多个用户控件。它们只是 UI 的方便分离,因此它们没有自己的 ViewModel。
ShellView 包含一个键盘(自定义用户控件),其中包含多个键盘按钮(自定义用户控件)。
我非常确定(因为我已经检查过)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 的主体中设置断点,我可以看到Click 被正确捕获,并且它包含指向RelayCommand 的点。但是,什么也没有发生:“Click(sender, new KeypadButtonEventArgs());”已被执行,但仅此而已。
为什么会这样?不应该执行 RelayCommand 中定义的目标函数吗?也许是与范围相关的问题?
提前致谢, 干杯, 吉安卢卡.
I am using the MVVM Light framework to build a SL4 application. My simple app is composed primarily by a single main view (shellView), which is divided into multiple UserControls. They are just a convenient separation of the UI, therefore they don't have their own ViewModel.
The ShellView contains a Keypad (custom usercontrol) that contains multiple KeypadButtons (custom usercontrols).
I am quite sure (because I've checked) that the DataContext is set properly and it is used by all usercontrols in the hierarchy. (ShellView's Datacontext is ShellViewModel, Keypad's DataContext is ShellViewModel, etc.).
In the ShellViewModel I have a ICommand (RelayCommand) that is named "ProcessKey".
In the Keypad control, I have something like:
<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>
The KeypadButton is basically a Grid that contains a Button. The MouseLeftButtonUp event is caught and a custom "Click" event is fired. Let me show you some code to explain easily what I am doing:
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; }
}
Now, if I set a breakpoint to the body of innerButton_Click, I can see the Click is properly caught and it contains points to the RelayCommand. However, nothing happens: "Click(sender, new KeypadButtonEventArgs());" is executed but nothing more.
Why is this behaving so? Shouldnt execute the target function that is defined in the RelayCommand? Is maybe a scope-related issue?
Thanks in advance,
Cheers,
Gianluca.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如其他评论所指出的,这可能与
Click
事件不是RoatedEvent
有关。作为一种快速技巧,您可以使用
MouseLeftButtonDown
而不是UserControl
上的Click
事件。您可以考虑的另一个选择是继承
Button
而不是UserControl
。 Silverlight Show 有一篇有关从 TextBox 继承的文章< /a> 这可能与此相关。As noted by other comments, this is probably related to the
Click
event not being aRoutedEvent
.As a quick hack you might be able to use
MouseLeftButtonDown
instead of theClick
event on yourUserControl
.Another option you could consider is inheriting from
Button
instead ofUserControl
. Silverlight Show has an article about inheriting from a TextBox that probably is relevant for this.路由事件应如下定义(请参阅文档):
Routed events should be defined like this (see documentation):