简体   繁体   中英

How to handle events for ItemClick in Sublist for UWP, MVVM?

I am new to UWP. I have a question which already has been asked here,

UWP: How to catch the click of a listview part from another listview in the viewmodel instead of code-behind?

However, none of the solutions really work. I have tested them out, "ElementName" can not find the Name for the parent ListView.

Restating the scenario here,

Let's say I have this skeleton. [Writing in pseudocode]

ViewModel{

   Collection< TypeT > CollectionParent{}
   RelayCommand_For_ChildList ChildItemClick; 
}

//TypeT is a model in here. 

TypeT{
    Collection<string> CollectionChild{}
}


<ListView Name="ParentListView" ItemSource="CollectionParent">

<ListView.ItemTemplate>
   <DataTemplate>
        <ListView Name="ChildListView" Command="{Binding ChildItemClick}" ItemSource="AnotherCollectionWithinTheFirstCollection">
</ListView>
   </DataTemplate>
</ListView.ItemTemplate>

</ListView>

When I would run this code, "Binding ChildItemClick" tries to find "ChildItemClick" inside TypeT , instead of ViewModel.

I want to catch this event inside a ViewModel. How can I do that, please suggest.

Thank you for reading.

If this is still a problem and you are interested in an answer here you go. I think you have many options here.

  • Like already mentioned you can use the behaviours SDK and you have to name the Child-ListView, so you can bind the SelctionChanged-Event to a Command

  • You can bind your selected item to an element of your class which provides the list und react in the setter method but remember that you need the call NotifyPropertyChanged

Those are the two simple optiones.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM