Windows App 8.1 StackPanel鼠标悬停

时间:2017-01-30 01:13:21

标签: c# wpf windows-8.1 mouseover stackpanel

我在Windows 8.1应用上有StackPanel,我想添加Mouse-Over功能。 如果我理解正确,Windows 8.1应用程序上的类似功能是PointerEntered/PointerExited。我怎样才能得到"项目" TextBlock的文本,例如这些函数的文本?

编辑:我基本上想要在鼠标悬停时获得TextBlock的句柄,这样我就可以获取或设置它的属性(文本等等)。

这是我的C#功能

private void itemGridView_PointerEntered(object sender, PointerRoutedEventArgs e)
    {

    }

和我的xaml代码:

<GridView
        x:Name="itemGridView"
        AutomationProperties.AutomationId="ItemGridView"
        AutomationProperties.Name="Items In Group"
        TabIndex="1"
        Grid.RowSpan="2"
        Padding="120,126,120,50"
        ItemsSource="{Binding Source={StaticResource itemsViewSource}}"
        SelectionMode="None"
        IsSwipeEnabled="false"
        IsItemClickEnabled="True"
        ItemClick="ItemView_ItemClick" PointerEntered="itemGridView_PointerEntered">
        <GridView.ItemTemplate>
            <DataTemplate>
                    <Grid Height="110" Width="480" Margin="10">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
                        <Border Background="{ThemeResource ListViewItemPlaceholderBackgroundThemeBrush}" Width="110" Height="110">
                            <Image x:Name="ImageItemName" Source="{Binding ImagePath}" Stretch="UniformToFill" AutomationProperties.Name="{Binding Title}"/>
                        </Border>
                        <StackPanel x:Name="stackPanel1" Grid.Column="1" VerticalAlignment="Top" Margin="10,0,0,0">
                            <TextBlock x:Name="ItemTitleText" Text="{Binding Title}" Style="{StaticResource TitleTextBlockStyle}" TextWrapping="NoWrap"/>
                            <TextBlock Text="{Binding Subtitle}" Style="{StaticResource CaptionTextBlockStyle}" TextWrapping="NoWrap"/>
                            <TextBlock x:Name="ItemDescText" Text="{Binding Description}" Style="{StaticResource BodyTextBlockStyle}" MaxHeight="60"/>
                        </StackPanel>
                    </Grid>
                </DataTemplate>
            </GridView.ItemTemplate>
            <GridView.Header>
            <StackPanel Width="480" Margin="0,4,14,0">
                <TextBlock x:Name="TitleTextBlock" Text="{Binding Subtitle}" Margin="0,0,0,20" Style="{StaticResource SubheaderTextBlockStyle}" MaxHeight="60"/>
                <Image x:Name="imageName" Source="{Binding ImagePath}" Height="400" Margin="0,0,0,20" Stretch="UniformToFill" AutomationProperties.Name="{Binding Title}"/>
                <TextBlock x:Name="DescTextBlock" Text="{Binding Description}" Margin="0,0,0,0" Style="{StaticResource BodyTextBlockStyle}"/>
            </StackPanel>
        </GridView.Header>
        <GridView.ItemContainerStyle>
            <Style TargetType="FrameworkElement">
                <Setter Property="Margin" Value="52,0,0,2"/>
            </Style>
        </GridView.ItemContainerStyle>
    </GridView>

1 个答案:

答案 0 :(得分:1)

使用属性Children获取UIElementCollection个子元素。

Panel.Children property. MSDN

    private void StackPanel_PointerEntered(object sender, PointerRoutedEventArgs e)
    {
        var values = new List<string>();
        var sp = sender as StackPanel;
        if (sp != null)
        {
            foreach (var child in sp.Children)
            {
                var tb = child as TextBlock;
                if (tb != null)
                {
                    values.Add(tb.Text);
                }
            }
        }
    }