I have the following list view in a user control. I would like to register a double click on the entire row. When a double click is registered, I would like to call an ICommand in my view model.
Does anyone know how I can do this?
I know there are other posts about this and I have tried the solutions found in them (using both MVVM Light and AttachedCommandBehavior) but either the double click is simply not registered or it tries to call the ICommand on my TVSeries object which is my model and which holds the data presented in the rows.
This is my list view:
<ListView Height="456"
HorizontalAlignment="Left"
Margin="12,12,0,0"
VerticalAlignment="Top"
Width="488"
ItemsSource="{Binding Path=Library}"
SelectedItem="{Binding Path=SelectedTVSeries}">
<ListView.View>
<GridView>
<GridViewColumn Header="Name"
Width="Auto">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Name}"
FontWeight="Bold" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Year"
Width="Auto">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Year}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Genre"
Width="Auto">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Genre}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
UPDATE
When using AttachedCommandBehavior I will get the following error:
NullReferenceException was unhandled
Object reference not set to an instance of an object.
The error occurs on the following lines (which are a part of AttachedCommandBehavior):
/// <summary>
/// Executes the strategy
/// </summary>
public void Execute()
{
strategy.Execute(CommandParameter);
}
I use ACB like below. First I declare my style for the ListViewItem.
<– language: xml –>
<UserControl.Resources>
<Style x:Key="LibraryListViewItemStyle"
TargetType="{x:Type ListViewItem}">
<Setter Property="acb:CommandBehavior.Event" Value="MouseDoubleClick" />
<Setter Property="acb:CommandBehavior.Action" Value="{Binding RelativeSource={RelativeSource AncestorType=ListView}, Path=DataContext.TVSeriesDoubleClickedCommand}" />
<Setter Property="acb:CommandBehavior.CommandParameter" Value="{Binding }" />
</Style>
</UserControl.Resources>
Then I set the ItemContainerStyle on my list view to the style.
<ListView Height="456"
HorizontalAlignment="Left"
Margin="12,12,0,0"
VerticalAlignment="Top"
Width="488"
ItemsSource="{Binding Path=Library}"
SelectedItem="{Binding Path=SelectedTVSeries}"
ItemContainerStyle="{StaticResource LibraryListViewItemStyle}">
...
</ListView>
Does anyone know how to fix this? This is really the closest I have been.
** UPDATE: Solution**
The above error occurred, because I have configured AttachedCommandBehavior to expect an action, but I had implemented a command. Changing from the following
<Setter Property="acb:CommandBehavior.Action" Value="{Binding RelativeSource={RelativeSource AncestorType=ListView}, Path=DataContext.TVSeriesDoubleClickedCommand}" />
To the following
<Setter Property="acb:CommandBehavior.Command" Value="{Binding RelativeSource={RelativeSource AncestorType=ListView}, Path=DataContext.TVSeriesDoubleClickedAction}" />
That solved the error.
However, I found that I prefer using an action so I kept acb:CommandBehavior.Action and changed my implementation:
public Action<object> TVSeriesDoubleClickedAction
{
get
{
return new Action<object>(tvSeries => Console.WriteLine("*** Double clicked fired: {0}", ((TVSeries)tvSeries).Name));
}
}
This would happen because you have your binding to your ICommand set incorrectly. I would suggest going back to one of the solutions you’ve tried where it was calling the ICommand on the TVSeries object and try changing your binding to something similar to this: