1

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.

Skynet094
  • 443
  • 4
  • 19
  • Why you don't set a `DataTemplate` for the sub `ListView` too? In the `DataTemplate ` you would be able to set the `Command`. If the control doesn't provide a `DependencyPropertie` for `ICommand` you could use https://github.com/Microsoft/XamlBehaviors – Briefkasten Jul 06 '18 at 06:41
  • Sorry I forgot to mention, actually, I am using a behaviour thing in the sublist , but the "Binding" command looks into the Model , not the ViewModel :( – Skynet094 Jul 06 '18 at 06:48
  • You could try something like that in your command binding, `"{Binding ElementName=ParentListView, Path=DataContext.ChildItemClick}"` – Briefkasten Jul 06 '18 at 06:51
  • I am trying to do that, but The "ParentListView" (I have named the parent list view) is not available as an ElementName from sublist – Skynet094 Jul 06 '18 at 07:11
  • It seems you are using behavior SDK in your code, do you have any other code that will affect the result? Could you provide a more completed sample to make us discuss this issue in the same direction? – Breeze Liu - MSFT Jul 09 '18 at 02:27

1 Answers1

0

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.

MelloPs
  • 135
  • 11