locked
Blend style events in user control. RRS feed

  • Question

  • I have created a button user control. This is meant to be a reusable control so I do not want to hard code values in this control.

    
    
    <UserControl x:Class="CommonControl.ctlButton"            
                 xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
                 xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions" 
                 xmlns:local="clr-namespace:CommonControl"
                 mc:Ignorable="d" 
                 d:DesignHeight="23" d:DesignWidth="80"
                  Margin="2">
        <UserControl.Template>
            <ControlTemplate TargetType="{x:Type UserControl}">
                <Button Name="button" >
                    <StackPanel Orientation="Horizontal">
                        <Image x:Name="image" Source="{TemplateBinding local:ctlButton.ImageSource}"/>
                        <TextBlock x:Name="label" Text="{TemplateBinding local:ctlButton.ButtonText}"/>
                    </StackPanel>
                </Button>
            </ControlTemplate>
        </UserControl.Template>
    </UserControl>
    


    I want to use the button user control in a main window and i want the user control to use blend style events as shown below. However the left mouse click event does not fire the method Update in my View Model.

    <cc:ctlButton Name="btnUpdate" ButtonText="Update" Width="120" Grid.Row="5" Grid.Column="3" HorizontalAlignment="Left" IsEnabled="{Binding Source={x:Reference ctl}, Path=DataContext.CanSave}">
                                <i:Interaction.Triggers>
                                    <i:EventTrigger EventName="MouseLeftButtonDown">
                                        <ei:CallMethodAction TargetObject="{Binding Source={x:Reference ctl}, Path=DataContext}" MethodName="Update"/>
                                    </i:EventTrigger>
                                </i:Interaction.Triggers>
                            </cc:ctlButton>


    Could anyone explain why this is so? How could this be corrected?


    • Edited by llkoolj Tuesday, January 15, 2013 4:19 AM
    • Moved by Sheldon _Xiao Tuesday, January 15, 2013 7:05 AM
    Tuesday, January 15, 2013 4:11 AM

Answers

  • Button uses MouseLeftButtonDown internally and does not pass it along, so if you clicked the button itself, the mouse down event does not go any further than the button.

    You could create your own event type on your UserControl and have the Button inside the UserControl trigger that even when clicked, then your trigger could react to your custom event.

    • Marked as answer by llkoolj Tuesday, January 15, 2013 11:13 PM
    Tuesday, January 15, 2013 2:14 PM
    Moderator

All replies

  • Hi llkoolj,

    I will move your thread to more approrate forum to get better answer.

    best regards,


    Sheldon _Xiao
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, January 15, 2013 7:04 AM
  • Button uses MouseLeftButtonDown internally and does not pass it along, so if you clicked the button itself, the mouse down event does not go any further than the button.

    You could create your own event type on your UserControl and have the Button inside the UserControl trigger that even when clicked, then your trigger could react to your custom event.

    • Marked as answer by llkoolj Tuesday, January 15, 2013 11:13 PM
    Tuesday, January 15, 2013 2:14 PM
    Moderator
  • Thanks. It worked. I made a minor change and used the PreviewMouseLeftButtonDown event on the use control
    Tuesday, January 15, 2013 11:05 PM