I have this listview:
<Page
x:Class="DataBase.ControlsPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
DataContext="{Binding CustomersViewModel, Source={StaticResource ViewModelLocator}}">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<ListView Name="HeaderList" ItemsSource="{Binding Customers}">
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<Border Background="Bisque">
<TextBlock Text="{Binding Name"/>
</Border>
<ListView Name="SubItemsList" ItemsSource="{Binding Project}" ItemClick="SubItemClick">
<x:String>SubItem 1</x:String>
<x:String>SubItem 2</x:String>
<x:String>SubItem 3</x:String>
</ListView>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
<x:String>Item 1</x:String>
<x:String>Item 2</x:String>
<x:String>Item 3</x:String>
</ListView>
</Grid>
</Page>
All I want is to catch the click of a subItem(ItemClick="SubItemClick">) in my CustomersViewModel. Is that possible? I know for the sub-items list items, the data is a Project which is just a data model, it does not contain any click handler. But, how can I catch the click in the view model and not in the code-behind?
Also I'm uploading a picture to visualise what I want:
What you really need is a
ClickCommand
in your viewmodel. But since theListView
control doesn't expose aItemClickCommand
, one common way is to use a behavior to establish the connection between yourClickCommand
and theItemClick
event.This particular behavior you are looking for is called
InvokeCommandAction
, which can be found in this nuget package.Basically the end result would look something like this -
As Justin XL said, the answer I was looking for is this:
ItemClick="{Binding DataContext.ClickCommand, ElementName=HeaderList}"