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?
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With