在itemscontroldatatemplate中未触发的XAML Textblock Tapped事件

本文关键字:XAML Textblock Tapped 事件 itemscontroldatatemplate | 更新日期: 2023-09-27 17:55:02

        <DataTemplate x:Key="nodeTrafficQualifierTemplate">
        <Grid Width="400" Margin="8">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"></ColumnDefinition>
                <ColumnDefinition Width="*"></ColumnDefinition>
                <ColumnDefinition Width="Auto"></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <TextBlock Grid.Column="0" TextWrapping="WrapWholeWords" Text="{Binding Name}" />
            <StackPanel Grid.Column="2" Orientation="Horizontal" HorizontalAlignment="Right" >
                <TextBlock Name="GreenQualifier"  Text="{StaticResource CircleOpen}" 
                       IsTapEnabled="True"
                       Tapped="TrafficLightQualifier_OnTapped"
                       FontFamily="{StaticResource SymbolThemeFontFamily}" 
                       FontSize="20" Margin="0,0,60,0" Foreground="Green"/>
            </StackPanel>
        </Grid>
    </DataTemplate>
private void TrafficLightQualifier_OnTapped(object sender, TappedRoutedEventArgs e)
    {
        // Never get here...
        var qualifier = ((TextBlock)sender).Name;
        switch (qualifier)
        {
            case "GreenQualifier":
                break;
        }
    }
事件处理程序永远不会被调用。除了使用图像控件而不是Textblock之外,我有几乎相同的东西,并且tap事件处理程序工作得很好。

Textblock的Loaded事件被触发并正常处理。

这与焦点有关吗?有什么办法能让它起作用吗?

谢谢!

在itemscontroldatatemplate中未触发的XAML Textblock Tapped事件

这绝对是一个边缘情况。我在我的文本块中使用的符号是"零宽度"字形,旨在相互覆盖,以制作单选按钮等。大多数的符号符号不是零宽度,他们工作得很好。解决方案是在Textblock中添加Width="40",然后事件处理程序就可以正常工作了。

尝试在构造函数中添加处理程序:

GreenQualifier.AddHandler(TappedEvent, new TappedEventHandler(TrafficLightQualifier_OnTapped), true);

对我来说,它工作得很好。

但是,一个假设是,ListViewDataTemplate被消耗的地方在eventhandler之前处理Tapped事件。检查你的ListView属性和我的。

我的ListView如下所示。您可以找到<!-- THIS IS WHERE I TESTED -->代码。

<ListView Grid.Row="0" x:Name="ListViewPages" HorizontalAlignment="Stretch" Background="{StaticResource LbLightShadowBrush}"
        VerticalAlignment="Stretch" BorderThickness="0" Margin="0,45,0,0"
        CanReorderItems="True" CanDragItems="True" AllowDrop="True" ItemContainerStyle="{StaticResource ListViewItemStyle1}" SelectionChanged="ListViewPages_OnSelectionChanged">
                        <ListView.ItemTemplate>
                            <DataTemplate>
                                <Border BorderBrush="White" BorderThickness="0,0,0,0" Height="60">
                                    <Grid Background="White">
                                        <StackPanel Orientation="Vertical" Margin="5">
                                            <TextBlock x:Name="TestBlock" Foreground="{StaticResource LbBackgroundBrush}" Text="{Binding Path=Title}" FontFamily="Global User Interface" Margin="0,3" Tapped="TestBlock_OnTapped" /> <!-- THIS IS WHERE I TESTED -->
                                            <TextBlock Foreground="{StaticResource LbLightTextBrush}" Text="{Binding Path=Data, Converter={StaticResource PropertyDataSummary}}"/>
                                        </StackPanel>
                                    </Grid>
                                </Border>
                            </DataTemplate>
                        </ListView.ItemTemplate>
                        <ItemsPanelTemplate>
                            <VirtualizingStackPanel Orientation="Vertical" />
                        </ItemsPanelTemplate>
                    </ListView>

并且,eventhandler只是通过断点进行检查。

        private void TestBlock_OnTapped(object sender, TappedRoutedEventArgs e)
    {
        Debug.WriteLine("tapped");
    }