如何检测由边框中嵌入的TextBlock生成的边框控件中的事件?

时间:2014-04-08 01:14:54

标签: c# xaml winrt-xaml

我有一个边境控制,"主持"一个TextBlock:

                <Border x:Name="VideosBorder" 
                    BorderBrush="Black" 
                    BorderThickness="1" 
                    Background="#FF77FF83" 
                    Tapped="Border_Tapped">

                <TextBlock x:Name="tbVideos" 
                           TextWrapping="Wrap" 
                           Text="Videos" 
                           TextAlignment="Center" 
                           FontWeight="Bold" 
                           FontSize="13.333" 
                           Foreground="Black" 
                           VerticalAlignment="Center" 
                           Tapped="Border_Tapped"/>
            </Border>


    private void Border_Tapped(object sender, Windows.UI.Xaml.Input.TappedRoutedEventArgs e)
        {
        if ( ( sender == VideosBorder ) || ( sender == tbVideos ) )
            {
            ...
            }
        else if ( ( sender == ImagesBorder ) || ( sender == tbImages ) )
            {
             ...
           }
        else if ( ( sender == ArticlesBorder ) || ( sender == tbArticles ) )
            {
            ...
            }
        else
            {
            ...
            }

        ...
        }

我想检测Border控件的所有区域上的任何Tapped事件。但是,如果TextBlock是Tapped,则Border不会检测Tapped事件。我通过实现两个Tapped事件(一个在Border上,一个在TextBlock上)解决了这个问题。是否可以仅检测Border Tapped事件并在TextBlock上发送事件以鼓泡到边框?

THX。

1 个答案:

答案 0 :(得分:1)

您只需要添加e.Handled = true;在Border_Tapped事件上。希望这个帮助

<Border x:Name="VideosBorder" Height="35" Width="200" BorderBrush="Red" BorderThickness="1" Background="#FF77FF83" Tapped="Border_Tapped">
    <TextBlock x:Name="tbVideos" TextWrapping="Wrap" Text="Videos" TextAlignment="Center" FontWeight="Bold" FontSize="13.333" Foreground="Black" VerticalAlignment="Center" />
</Border>

  private void Border_Tapped(object sender, TappedRoutedEventArgs e)
    {
        e.Handled = true;
        VideosBorder.BorderThickness = new Thickness(5);
    }