开发者

How can I have MouseUp and MouseDoubleClick events on WPF Datagrid

Here is the problem. I am using MVVM on a WPF project and using MVVM light. I have a grid in a user control that returns results from a search. I want the users to be able to click on the grid and have the row available for the parent view to use (from a menu) and have it so users can double click on the row and open in a new "window". Individually I have these items work properly, however I can not get bot开发者_如何学Ch to work. I am trying to tie 1 command to the MouseUp and another to MouseDoubleClick but the MouseDoubleClick event never gets fired. How can I be able to use the mouseUp and MouseDoubleClick events in a MVVM setup? or any other suggestions to be able to select a row from the datagrid to be available to menu items and to be able to doubleclick on.


Using MVVMLight the eventtocommand will get you selectionchanged and mousedoubleclicked events.


I think you're going to have to manually detect a double-click.

dblClickTimeOut = null;
row.onmouseup = function() {
    if( dblClickTimeOut == null)
        dblClickTimeOut = setTimeout("dblClickTimeOut = null; selectRow('"+this.id+"');",200);
    else {
        // double-click stuff
    }
}
selectRow = function() {
    // single-click stuff
}


For those who love short ways (also mvvm friendly!), define the interactivity in your xaml-class:

xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"

And in your Grid:

<Grid>
  <Grid.InputBindings>
     <MouseBinding MouseAction="LeftDoubleClick" Command="{Binding ElementName=xyz, Path=DataContext.MouseDoubleClick}"/>
  </Grid.InputBindings>
  <i:EventTrigger EventName="MouseUp">
     <cmd:EventToCommand Command="{Binding ElementName=xyz, Path=DataContext.MouseUpEvent}"/>
  </i:EventTrigger>
</Grid>

Hope that helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜