0
votes

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.

2

2 Answers

1
votes

As noted by other comments, this is probably related to the Click event not being a RoutedEvent.

As a quick hack you might be able to use MouseLeftButtonDown instead of the Click event on your UserControl.

<!-- Kinda Hacky Click Interception -->
<controls:KeypadButton x:Name="testBtn" Text="Hello">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="MouseLeftButtonDown">
                <GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding PressStandardKeyCommand}" />
            </i:EventTrigger>
        </i:Interaction.Triggers>
</controls:KeypadButton>

Another option you could consider is inheriting from Button instead of UserControl. Silverlight Show has an article about inheriting from a TextBox that probably is relevant for this.

0
votes

Routed events should be defined like this (see documentation):

public static readonly RoutedEvent TapEvent = EventManager.RegisterRoutedEvent(
    "Tap", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(MyButtonSimple));

// Provide CLR accessors for the event
public event RoutedEventHandler Tap
{
        add { AddHandler(TapEvent, value); } 
        remove { RemoveHandler(TapEvent, value); }
}