Windows App 8.1 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>

使用属性Children来获取子元素的UIElementCollection

面板.儿童属性。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);
                }
            }
        }
    }

相关内容

  • 没有找到相关文章

最新更新