【问题标题】:How to handle events for ItemClick in Sublist for UWP, MVVM?如何处理 UWP、MVVM 的子列表中的 ItemClick 事件?
【发布时间】:2018-12-14 12:50:33
【问题描述】:

我是 UWP 的新手。我有一个问题已经在这里问过了,

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

但是,没有一个解决方案真正有效。我已经对它们进行了测试,“ElementName”找不到父 ListView 的名称。

在这里重述场景,

假设我有这个骨架。 [用伪代码编写]

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>

当我运行此代码时,“Binding ChildItemClick”会尝试在 TypeT 中查找“ChildItemClick”,而不是 ViewModel。

我想在 ViewModel 中捕获此事件。我该怎么做,请建议。

感谢您的阅读。

【问题讨论】:

  • 你为什么不为子ListView 也设置一个DataTemplate?在DataTemplate 中,您可以设置Command。如果控件没有为ICommand 提供DependencyPropertie,您可以使用github.com/Microsoft/XamlBehaviors
  • 对不起,我忘了提,实际上,我在 sublist 中使用了一个行为的东西,但是“Binding”命令查看的是 Model,而不是 ViewModel :(
  • 您可以在命令绑定中尝试类似的操作,"{Binding ElementName=ParentListView, Path=DataContext.ChildItemClick}"
  • 我正在尝试这样做,但是“ParentListView”(我已命名父列表视图)不能作为子列表中的 ElementName 使用
  • 您的代码中似乎使用了行为SDK,您是否还有其他代码会影响结果?您能否提供一个更完整的示例,让我们朝着同一个方向讨论这个问题?

标签: c# mvvm uwp uwp-xaml uwp-navigation


【解决方案1】:

如果这仍然是一个问题,并且您对答案感兴趣,请点击此处。 我想你在这里有很多选择。

  • 如前所述,您可以使用行为 SDK,并且必须命名 Child-ListView,以便将 SelctionChanged-Event 绑定到命令

  • 您可以将所选项目绑定到类的元素,该元素提供列表并在 setter 方法中做出反应,但请记住您需要调用 NotifyPropertyChanged

这是两个简单的选项。

【讨论】:

    猜你喜欢
    • 2017-10-11
    • 2018-12-27
    • 2019-05-04
    • 1970-01-01
    • 2014-06-29
    • 1970-01-01
    • 2011-08-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多