UWP 如何获取 ListviewItem 当前单击和以前单击 [英] UWP How to get ListviewItem Currently clicked and Previously clicked

查看:24
本文介绍了UWP 如何获取 ListviewItem 当前单击和以前单击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am developing UWP App, I have a Listview bind with a class, wherein upto 10 items. Listview have a DataTemplate and a Usercontrol is inside this DataTemplate.

我想点击任何项目,这样动画(故事板)应该开始,ColorBand 应该向右扩展,当我现在点击另一个项目时,扩展的(之前点击的)ColorBand 应该折叠,当前点击的项目的 ColorBand 应该扩展.

I want to click on any item so the animation (storyboard) should start and the ColorBand should expand toward right and when I click on another item now the expanded (previously clicked) ColorBand should collapse and the current clicked item's ColorBand should expand.

如果我将这个 ColorBand 放在 Listviewitem 样式中并使用可视状态管理器,这种方法是可行的,但实际上我需要在运行时通过类动态放置边框颜色和角半径等,并且如果用户想要改变颜色等......所以它必须通过绑定.

This approach can be possible if I put this ColorBand inside the Listviewitem Style and use the Visual State Manager, but actually I need to put the border color and corner radius etc dynamically via class in runtime and also have the Edit option if the user want to change the color etc... so it must be via binding.

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

So I need to run animation on currently clicked item and the previously clicked item simultaneously. plz help, I m stuck due to this.

推荐答案

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

      }

这篇关于UWP 如何获取 ListviewItem 当前单击和以前单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆