【问题标题】:UWP - How to select ListViewItem when a button inside it is clickedUWP - 单击其中的按钮时如何选择 ListViewItem
【发布时间】:2018-05-24 19:13:45
【问题描述】:

我有一个 ListView,其中 ListViewItems 有按钮,在 ListView.ItemTemplate 中定义。如何在此类按钮的 Click 事件中选择 ListViewItem?我知道如何在 WPF 中做到这一点,但无法为 UWP 计算,因为 UWP 样式不支持触发器。

【问题讨论】:

    标签: uwp


    【解决方案1】:

    在按钮点击事件中添加:

    listView.SelectedItem = ((FrameworkElement)sender).DataContext;
    

    【讨论】:

    • 希望有没有code-behind的解决方案,但是atm只能做code-behind。你的或这个: var item = (Button)sender).DataContext; var container = (ListViewItem)ListviewTankVIEW.ContainerFromItem(item); container.IsSelected = true;
    【解决方案2】:

    您将不得不使用委托命令

    以下示例向您展示如何完成此操作

    https://code.msdn.microsoft.com/windowsapps/How-to-bind-command-to-a-299f7759

    但一个更简单的例子是使用模板 10,它已经为您创建了一个委托命令。

    https://github.com/Windows-XAML/Template10/wiki/MVVM#delegatecommand

    要了解有关模板 10 的更多信息,请访问

    https://mva.microsoft.com/en-us/training-courses/getting-started-with-template-10-16336

    【讨论】:

    • 抱歉,我的问题可能不是很清楚,我需要这个:ListViewItems 在其中一列中有按钮。单击此类按钮会引发 Prism Command,但它要么不选择 ListViewItem,要么(在更改某些 ListView 属性后) - 它首先触发一个命令,然后选择新的 ListViewItem。但我需要先选择 ListViewItem,然后触发命令
    猜你喜欢
    • 2020-07-06
    • 1970-01-01
    • 1970-01-01
    • 2020-05-30
    • 1970-01-01
    • 2014-08-30
    • 2011-09-09
    • 2014-04-09
    • 1970-01-01
    相关资源
    最近更新 更多