如何在列表视图中制作幻灯片效果? - UWP

时间:2018-04-15 17:55:26

标签: xaml mvvm uwp visual-studio-2017

我不知道如何很好地完成这个效果,而且我知道如何捕获StackPanel以实现效果。最后帮助我将是非常有帮助的。我想要的是从ListView中隐藏所有StackPanel并仅显示被点击的元素,如果它失去焦点,它将再次隐藏。这是必不可少的。

enter image description here

<ListView Grid.Row="1" ItemsSource="{Binding listOfSomething, Mode=TwoWay}" SelectionChanged="ListView_SelectionChanged">
    <!-- rest of code -->
    <ListView.ItemTemplate>
        <DataTemplate>
            <Grid HorizontalAlignment="Stretch" Margin="0,2,0,2">
                <!-- rest of code -->
                <StackPanel Grid.Column="2" Grid.RowSpan="2" Background="#FFF" Margin="5,0,0,0">
                    <Grid Background="Transparent" Height="52">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="*" />
                        </Grid.ColumnDefinitions>
                        <Button Grid.Column="0" Background="Transparent" Padding="0,0,0,0">
                            <StackPanel Padding="0,0,0,0" Margin="0,0,0,0">
                                <Image Source="ms-appx:///Assets/plus.png" Width="32" Height="32" />
                            </StackPanel>
                        </Button>
                        <Button Grid.Column="1" Background="Transparent" Padding="0,0,0,0">
                            <StackPanel Padding="0,0,0,0" Margin="0,0,0,0">
                                <Image Source="ms-appx:///Assets/delete.png" Width="32" Height="32"/>
                            </StackPanel>
                        </Button>
                    </Grid>
                </StackPanel>
            </Grid>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

1 个答案:

答案 0 :(得分:1)

我采用这种方法,但我认为它不推荐用于长列表,因为它试图暂停/维护视图,但根据您期望的结果/效果,它值得一试:

首先,在Page.xaml

中创建两个DataTemplates
<Page.Resources>
     <DataTemplate x:Name="normalTemplate" x:Key="normalTemplate">
         <!-- Your template view when item is NOT clicked/selected -->
     </DataTemplate>

     <DataTemplate x:Name="selectedTemplate" x:Key="selectedTemplate">
         <!-- Your template view when item is clicked/selected -->
     </DataTemplate>
</Page.Resources>

使用以下命令修改ListView控件:

<ListView Grid.Row="1" ItemsSource="{Binding listOfSomething, Mode=TwoWay}" SelectionChanged="ListView_SelectionChanged" ItemTemplate="{StaticResource normalTemplate}" />

然后在ListView_SelectionChanged方法上插入/添加以下代码行:

try
{  
     // Assign DataTemplate for selected items
     foreach (var item in e.AddedItems)
     {
        ListViewItem lvi = (sender as ListView).ContainerFromItem(item) as ListViewItem;
        lvi.ContentTemplate = (DataTemplate)this.Resources["selectedTemplate"];
     }
     //Remove DataTemplate for unselected items
     foreach (var item in e.RemovedItems)
     {
         ListViewItem lvi = (sender as ListView).ContainerFromItem(item) as ListViewItem;
         lvi.ContentTemplate = (DataTemplate)this.Resources["normalTemplate"];
     }
}
catch (Exception ex) 
{ }