【问题标题】:UWP How to get ListviewItem Currently clicked and Previously clickedUWP如何获取当前单击和先前单击的ListviewItem
【发布时间】:2016-06-26 07:54:05
【问题描述】:

我正在开发 UWP 应用程序,我有一个与一个类绑定的 Listview,其中最多 10 个项目。 Listview 有一个 DataTemplate 和一个 Usercontrol 在这个 DataTemplate 里面。

我想单击任何项​​目,因此动画(故事板)应该开始并且 ColorBand 应该向右展开,当我现在单击另一个项目时,展开的(先前单击的)ColorBand 应该折叠并且当前单击的项目的 ColorBand 应该展开.

如果我将此 ColorBand 放在 Listviewitem 样式中并使用 Visual State Manager,则可以使用此方法,但实际上我需要在运行时通过类动态放置边框颜色和角半径等,并且如果用户想要更改颜色等...所以它必须通过绑定。

所以我需要同时在当前点击的项目和之前点击的项目上运行动画。请帮忙,我因此被卡住了。

【问题讨论】:

  • 我认为您可以在列表框样式中绑定 ColorBand 值。你试过了吗?
  • 如果没有,您将在选择更改事件中获得添加的项目和删除的项目(当前选择和上一个项目)。你也可以试试
  • 你能解释一下如何绑定listview样式以及如何获取添加和删除的项目吗??? :)
  • 您是否绑定了 ColorBand 值?
  • 是绑定 ColorBand 值

标签: windows-runtime windows-phone-8.1 win-universal-app universal


【解决方案1】:
<VisualStateGroup x:Name="SelectionStates">
                                    <VisualState x:Name="Unselected"/>
                                    <VisualState x:Name="Selected">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="InnerGrid">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{Binding ColorBand}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="White"/>
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="FontWeight" Storyboard.TargetName="ContentPresenter">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="ExtraBold"/>
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>

                                </VisualStateGroup>

使用 SelectionChanged 方法

     private async void Selectionchanged(object sender, SelectionChangedEventArgs args)
        {
        foreach(var item in args.AddedItems)
        {
ListViewItem item = (sender as ListView).ContainerFromItem(item) as ListViewItem;
    // you will get slected item here. Use that item to get listbox item
        }
    if(args.RemovedItems!=null)
    {
        foreach(var item in args.RemovedItems)
        {
    //You will get previosly selcted item here
ListViewItem item = (sender as ListView).ContainerFromItem(item) as ListViewItem
        }
    }

      }

【讨论】:

  • 试试这个。我已将 ColorBand 值绑定到背景属性。稍后我会发布其他方法
  • 没有。首先视觉状态令人困惑在哪里使用,所以我离开了视觉状态部分,但是我使用的 SelectionChanged 方法和 args.RemovedItem 的第二个条件始终为空......所以它不起作用:(跨度>
  • 当您更改选择时,先前选择的项目将出现在 RemovedItem 列表中。所以第一次它会是空的。
  • 奥普斯。我为 ListBox 编写了代码。我编辑了 ListView 的答案。请立即查看
  • 这不是问题所在,我已将 Listbox 更改为 Listview,但我将调试点放在了 removedItem 条件下,但它始终为空,并且调试器没有指向/命中 RemovedItem 条件内的数据。 :(
【解决方案2】:

我认为获取先前单击的项目的最简单方法是通过具有两个属性的 c# 代码。而且你可以随心所欲地使用这个项目。使用选择更改事件,当它引发时,您应该将此选定项目添加到新属性中,如下所示:

public object PreviouslySelectedItem {get; set;}
public object CurrentlySelectedItem 
{
get;
set{
       PreviouslySelectedItem = CurrentlySelectedItem ;
       CurrentlySelectedItem = value;
    }
}

private async void Selectionchanged(object sender, SelectionChangedEventArgs args)
    {
        CurrentlySelectedItem = (sender as ListBox).SelectedItem;

        // some handling with null cases and etc.
        // now you can apply your animation on your two items
    }

【讨论】:

  • 但是 RemovedItem 列表会给出之前选择的项目。为什么还要为此使用两个额外的属性?
  • 我说这是最简单的方法,当然如果你有 API 知识,你也可以使用它。大多数情况下,当我知道如何处理复杂性 O(1) 时,我会使用自己的逻辑,因为这给了我最佳实践和对代码的控制权,如果我不知道如何做我需要的事情我阅读了一些 API 文档
猜你喜欢
  • 1970-01-01
  • 2020-07-06
  • 2020-05-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多