Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Command for DoubleClick on ListView's GridView Row

Tags:

c#

mvvm

wpf

Within my WPF - MVVM application, I have a ListView in which there's a GridView. On Double clicking a row in the GridView, I want to read the text in a column in that row.

I want to use commands to maintain the MVVM pattern.

I know that one can use InputBindings to wire up a command for DoubleClick event. but there's no InputBindings for GridView.

Any idea how to achieve the functionality??

Thanks


Note - Want to know whether the above can be achieved by purely using commands - without code-behind for DoubleClick event handling?

like image 926
KhannaB7 Avatar asked Mar 18 '23 05:03

KhannaB7


2 Answers

I used a CellTemplate for a column within my GridView, in that celltemplate I provided InputBindings - MouseBinding for Double Click event. This turns the double click event into a command & then i send the Cell's text as a command parameter.

<ListView x:Name="listview1" ItemsSource="{Binding DataCollection}"  >

    <ListView.View>
        <GridView ColumnHeaderContainerStyle="{StaticResource ColumnHeaderStyle}">

            <GridViewColumn Header="ID"  Width="auto" DisplayMemberBinding="{Binding ID}" />
            <GridViewColumn Header="PrimaryFile"   Width="auto"  >
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding PrimaryFile}">
                            <TextBlock.InputBindings>
                                <MouseBinding Gesture="LeftDoubleClick" Command="{Binding Path=DataContext.ShowFileCommand, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" CommandParameter="{Binding PrimaryFile}"/>
                            </TextBlock.InputBindings>
                        </TextBlock>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
        </GridView>
    </ListView.View>

</ListView>

Here, the usercontrol's datacontext was set to a viewmodel, and the ShowFileCommand was exposed on my viewmodel.

So on double clicking Cells in 2nd column, command was raised along with the text in the cell as command parameter

like image 66
KhannaB7 Avatar answered Mar 20 '23 11:03

KhannaB7


To get KhannaB7's suggestion to work on all columns within you grid view you will need to do this

Set a DataTemplate in your resources

  <UserControl.Resources>
        <ResourceDictionary>
            <DataTemplate x:Key="GridViewCellTemplateStyle">
                <TextBlock Text="{Binding}">
                    <TextBlock.InputBindings>
                        <MouseBinding Gesture="LeftDoubleClick" Command="{Binding DataContext.CommandDoubleClick, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListView}}}"/>
                    </TextBlock.InputBindings>
                </TextBlock>
            </DataTemplate>
        </ResourceDictionary>
    </UserControl.Resources>

Create your grid view and make the columns inherit this datatemplate

 <ListView>
            <ListView.View>
                <GridView>
                    <GridViewColumn Width="Auto" Header="Column1" CellTemplate="{StaticResource GridViewCellTemplateStyle}"/>
                    <GridViewColumn Width="Auto" Header="Column2" CellTemplate="{StaticResource GridViewCellTemplateStyle}"/>
                    <GridViewColumn Width="Auto" Header="Column3" CellTemplate="{StaticResource GridViewCellTemplateStyle}"/>
                    <GridViewColumn Width="Auto" Header="Column4" CellTemplate="{StaticResource GridViewCellTemplateStyle}"/>
                </GridView>
            </ListView.View>
        </ListView>

Of course you can put what ever you want in your template. mine just shows how to make a double click for a grid view column row

like image 32
New Bee Avatar answered Mar 20 '23 10:03

New Bee