UWP如何获取ListviewItem当前点击和之前点击

UWP How to get ListviewItem Currently clicked and Previously clicked

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

我想点击任何项目,这样动画(情节提要)应该开始,ColorBand 应该向右扩展,现在当我点击另一个项目时,展开的(之前点击的)ColorBand 应该折叠,当前点击项目的 ColorBand应该扩大。

如果我将这个 ColorBand 放在 Listviewitem 样式中并使用视觉状态管理器,这种方法是可行的,但实际上我需要通过 class 在 [=20 中动态放置边框颜色和角半径等=]时间,如果用户想要更改颜色等,还有编辑选项...因此必须通过绑定。

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

<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
        }
    }

      }

我认为获取之前点击的项目的最简单方法是通过具有两个属性的 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
    }