【问题标题】:How to have a ListBox child item event not call parent event?如何让 ListBox 子项事件不调用父事件?
【发布时间】:2014-11-09 21:04:54
【问题描述】:

问题

当我单击 ListView 项目时,它会调用“Tapped”事件来导航到另一个页面。我在 ItemTemplate 中有一个 Up Vote 事件,当他们调用该特定事件时,我不想调用 ListView 的点击事件。知道我该怎么做吗?

ListView XAML:

父事件“listboxFeedbackItem_Tapped”在任何时候单击列表视图的任何部分时都会发生

   <Grid x:Name="gridMainData" Grid.Row="2">
         <ProgressBar x:Name="prgBar" IsIndeterminate="True" VerticalAlignment="Top" Visibility="{Binding Path=FeedbackVM.IsLoading, Mode=TwoWay}"/>
          <ListView ItemTemplate="{StaticResource FeedbackTemplate}" ItemsSource="{Binding FeedbackVM.FeedbackCollection}" Tapped="listboxFeedbackItem_Tapped"/>
   </Grid>

ItemTemplate Xaml:

事件“UpVoteItem_Tap”不应触发“listboxFeedbackItem_Tapped”

<DataTemplate x:Key="FeedbackTemplate">
    <StackPanel Orientation="Horizontal">
        <TextBlock Margin="0,0,30,0" Text="{Binding UpVotes}" Tapped="UpVoteItem_Tap"/>
    </StackPanel>
</DataTemplate>

也许 C# 中有一种方法可以防止后续事件的发生? 谢谢,我仍在努力解决 XAML。

【问题讨论】:

    标签: c# xaml windows-store-apps winrt-xaml


    【解决方案1】:

    当您收到 UpVote 被点击事件时,您可以通过设置 e.Handled=true 告诉它不要将该事件传递给父列表视图:

    void UpVoteItem_Tap(object sender, TappedRoutedEventArgs e)
    {
        // Processing here
        ...
    
        // don't send event to parent
        e.Handled = true;
    }
    

    【讨论】:

    • B-E-A-Utiful。正是我想要的。谢谢老哥!
    猜你喜欢
    • 1970-01-01
    • 2021-11-03
    • 1970-01-01
    • 1970-01-01
    • 2021-07-18
    • 2018-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多