Handling Mouse Events in MVVM in WPF

倖福魔咒の 提交于 2019-12-02 19:45:25
user3386790

@harjeet you need to use the below structure for Image MouseUp i.e instead of using "Image" try to use "Hyperlink command property" in TextBlock:

<TextBlock Panel.ZIndex="990"
       Canvas.Right="30"
       Canvas.Left="498"
       Canvas.Top="4">
  <Hyperlink TextDecorations="None"
         Command="{Binding CloseLoginSettingPopup}">  
  <Image  Cursor="Hand"
        x:Name="Pop2"
        Source="/img/close1.png"
        Height="40"
        Width="40"
        Panel.ZIndex="990"
        Canvas.Right="30"
        Canvas.Left="498"
        Canvas.Top="4" />
  </Hyperlink>
</TextBlock>

Instead of writing you own behavior and calling commands from it, you can leverage the EventTriggers and Interactivity to bind the event action to the command.

here is simple example of doing it

http://www.c-sharpcorner.com/Blogs/11789/example-of-eventtrigger-in-mvvm-application.aspx

as described in the example if you want to fire command on MouseUp event on your rectangle you can just do:

<Rectangle >
  <i:Interaction.Triggers>
    <i:EventTrigger EventName="MouseUp">
      <i:InvokeCommandAction Command="{Binding MyCommand}"/>
    </i:EventTrigger>
  </i:Interaction.Triggers>
</Rectangle >
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!