<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;
}
}
The event handler simply never gets called. I had pretty much the same thing as this except using Image control instead of Textblock, and the Tapped event handler worked fine.
The Loaded event for the Textblock fires and is handled without problems.
Is this related to focus? Is there any way to get this to work?
Thanks!
For me, it works fine.
But, one assumption is that, ListView
or where your DataTemplate
is consumed handles Tapped
event before eventhandler. Check your ListView
attributes and mine.
My ListView is as below. You can find <!-- THIS IS WHERE I TESTED -->
code.
<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>
And, eventhandler is simply checked by breakpoint.
private void TestBlock_OnTapped(object sender, TappedRoutedEventArgs e)
{
Debug.WriteLine("tapped");
}