Handling Mouse Events in MVVM in WPF

后端 未结 2 570
清歌不尽
清歌不尽 2020-12-28 22:17

I am Using MVVM with WPF. i am halted in a way, i want your views about this. i am firing Mouse Events using MouseBehaviour.cs class , Is there any other way to handle Mouse

相关标签:
2条回答
  • 2020-12-28 22:22

    @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>
    
    0 讨论(0)
  • 2020-12-28 22:44

    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 >
    
    0 讨论(0)
提交回复
热议问题