【发布时间】:2014-07-30 02:28:08
【问题描述】:
我有一个UserControl 和一个订阅Holding 事件的Grid。问题是Holding 事件针对我定位的项目以及ListView 中的其他一些项目触发。顺便说一句,我将控件用作 DataTemplate。
<ListView ItemsSource="{Binding ...}" Margin="0, 0, 0, 0">
...
<ListView.ItemTemplate>
<DataTemplate>
<local:MyUserControl/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
用户控制代码隐藏:
private bool isDescriptionVisible = false;
private void Grid_Holding(object sender, HoldingRoutedEventArgs e)
{
if (!isDescriptionVisible)
{
DescriptionFadeIn.Begin();
isDescriptionVisible = true;
}
}
private void Grid_Tapped(object sender, TappedRoutedEventArgs e)
{
if (isDescriptionVisible)
{
DescriptionFadeOut.Begin();
isDescriptionVisible = false;
}
}
用户控制内容:
<Grid.Resources>
<Storyboard x:Name="FadeIn">
<DoubleAnimation Storyboard.TargetProperty="Opacity" Storyboard.TargetName="DescriptionLayer"
Duration="0:0:0.3" To=".8"/>
</Storyboard>
<Storyboard x:Name="FadeOut">
<DoubleAnimation Storyboard.TargetProperty="Opacity" Storyboard.TargetName="DescriptionLayer"
Duration="0:0:0.3" To="0"/>
</Storyboard>
</Grid.Resources>
<Grid Margin="0, 0, 0, 48" Holding="Grid_Holding" Tapped="Grid_Tapped">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="1*"/>
</Grid.RowDefinitions>
<Image Source="{Binding Img}" Stretch="UniformToFill" Height="240" Width="450"/>
<Grid x:Name="DescriptionLayer" Background="Black" Opacity="0">
<TextBlock Text="{Binding Description}" FontSize="16" TextWrapping="Wrap" Margin="0, 9, 0, 0" MaxHeight="170" TextTrimming="CharacterEllipsis"/>
</Grid>
<StackPanel Grid.Row="1" Margin="12">
<TextBlock Text="{Binding Author}" FontSize="16"/>
<TextBlock Text="{Binding Title}" FontSize="18"/>
</StackPanel>
</Grid>
我无法在 DataTemplate 中包含的项目上使用情节提要,因此我不得不将其内容移动到 UserControl。
这个问题是否与虚拟化有关?我该如何解决这个问题?
替代品也可以。
更新:
一些 SO 帖子建议回收模式导致物品被重复使用。我已经添加
VirtualizingStackPanel.VirtualizationMode="Standard" 到我的 ListView 但问题出人意料地仍然存在。
所以现在我需要想办法防止其他项目重复相同的不透明度值(它不是数据绑定的,因为它是通过情节提要设置的)。
更新 2:
现在,在按住显示的项目时淡入的描述在它消失时完全消失:
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
【问题讨论】:
-
嗯...这篇文章描述了一个类似的问题:stackoverflow.com/questions/9487458/…。我在谷歌搜索“listview 虚拟化回收”后发现了这篇文章
标签: c# xaml windows-phone windows-phone-8.1