UWP How to get ListviewItem Currently clicked and Previously clicked

本小妞迷上赌 提交于 2019-12-08 14:31:28
<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>

Using SelectionChanged method

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

      }

I think the most easiest way to get previously clicked item is via c# code with two properties. And than you can do with this item whatever you want. Use selection changed event and when it raise you should add this selected item to a new properties something like this:

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
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!