gpt4 book ai didi

c# - 保持事件在 ListView 中未触及的项目上触发

转载 作者:太空狗 更新时间:2023-10-29 23:31:04 25 4
gpt4 key购买 nike

我有一个 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 中包含的项目上使用 Storyboard,因此我不得不将其内容移动到 UserControl。

这个问题与虚拟化有关吗?我该如何解决这个问题?

替代品会做。

更新:

一些 SO 帖子建议回收模式导致元素被重复使用。我已经添加VirtualizingStackPanel.VirtualizationMode="Standard" 添加到我的 ListView,但问题仍然存在。

所以现在我需要找出一种方法来防止其他项目重复相同的不透明度值(它不是数据绑定(bind)的,因为它是通过 Storyboard设置的)。

更新 2:

现在,在按住显示的项目时淡入的描述在离开 View 时完全消失:

<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>

最佳答案

我找到了解决方法;我添加了一个名为 DescriptionLayerOpacity 的属性到我的模型并将其设置为默认值 0当我向我的 ObservableCollection 添加新项目时在我的 View 模型中。

我还添加了 2 向绑定(bind)来更改源属性 ( DescriptionLayerOpacity ),因此 View 会随着 Storyboard 所做的更改而更新:

<Grid ... Opacity="{Binding DescriptionLayerOpacity, Mode=TwoWay}">
<TextBlock .../>
</Grid>

简而言之,所有 UserControl 的数据都必须进行数据绑定(bind),以避免在 ListView 的其他项目中重复。

这确实不是一个优雅的解决方案,而且还没有经过全面测试。在我找到真正的解决方案之前,这就足够了。

更新:未完全正常工作

我最近发现有些项目在选择其他项目时没有响应。为了让这些项目响应,我必须在 tap+hold 事件之前点击(顺便说一下,这两个事件是互斥的事件)。

更新 2:

删除 if 后似乎一切正常代码隐藏中的语句。但是绑定(bind)到模型中的不透明度属性以使其正常工作仍然是一个糟糕的解决方案。

关于c# - 保持事件在 ListView 中未触及的项目上触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25027842/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com