【问题标题】:How to allow manipulations within ListView/GridView Item controls while allowing scroll and cross-slide manipulations on the ListView/GridView?如何允许在 ListView/GridView 项控件中进行操作,同时允许在 ListView/GridView 上进行滚动和交叉滑动操作?
【发布时间】:2013-01-04 07:35:31
【问题描述】:

具体来说,我有一个自定义用户控件,它接收操作事件以滚动自定义 DirectX 控件。此控件和其他类似控件是 GridView 中的项。我希望 GridView 能够通过默认的 TranslateRailsX 操作水平滚动,而控件应该能够接收 TranslateRailsY 操作事件。

到目前为止,在我的实验中,通过将控件的操作模式设置为 System,我可以让 GridView 滚动工作,但控件不会接收任何操作事件。通过将控件的操作模式设置为 All、TranslateY 或 TranslateRailsY,我可以让我的自定义控件接收操作事件,但 GridView 触摸滚动将不起作用。

我怎样才能允许这两种操作?

【问题讨论】:

    标签: c# xaml windows-runtime windows-store-apps


    【解决方案1】:

    8.0 无法做到这一点(对于 8.1 滚动到 EDIT)。您需要实现自己的 ScrollViewer 以在 GridView 模板中使用,或者在 GridView 顶部放置一个图层,并将所有输入调用作为 I suggested before 中继。实际上,也许实现您自己的 ScrollViewer 版本并不难(一个带有一个子项的 Canvas 控件,它在操作事件的子项上调用 Canvas.SetLeft)。

    我有一个可能对您有用的新想法是在您的 DirectX 控件前面放置另一个 ScrollViewer,并像使用操作事件一样使用它的 ViewChanged 事件 - 请检查:

    XAML

    <Page
        x:Class="App84.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:App84"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d">
        <Grid
            Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
            <GridView>
                <Grid
                    Width="300"
                    Height="300">
                    <Rectangle
                        x:Name="DirectXControlPlaceholder"
                        Width="300"
                        Height="300"
                        Fill="Yellow" />
                    <ScrollViewer
                        x:Name="ManipulationCaptureScrollViewer"
                        Style="{StaticResource VerticalScrollViewerStyle}"
                        VerticalScrollBarVisibility="Hidden"
                        ViewChanged="ScrollViewer_OnViewChanged">
                        <Rectangle
                            Width="200"
                            Height="10000"
                            Fill="Transparent"/>
                    </ScrollViewer>
                    <TextBlock
                        x:Name="OffsetTextBlock"
                        Foreground="Black"
                        FontSize="24"
                        VerticalAlignment="Center"
                        HorizontalAlignment="Center"
                        />
                </Grid>
                <Rectangle
                    Width="300"
                    Height="300"
                    Fill="GreenYellow" />
                <Rectangle
                    Width="300"
                    Height="300"
                    Fill="LimeGreen" />
                <Rectangle
                    Width="300"
                    Height="300"
                    Fill="Red" />
                <Rectangle
                    Width="300"
                    Height="300"
                    Fill="OrangeRed" />
                <Rectangle
                    Width="300"
                    Height="300"
                    Fill="DarkOrange" />
                <Rectangle
                    Width="300"
                    Height="300"
                    Fill="Orange" />
            </GridView>
        </Grid>
    </Page>
    

    背后的代码

    using Windows.UI.Xaml.Controls;
    
    namespace App84
    {
        public sealed partial class MainPage : Page
        {
            public MainPage()
            {
                this.InitializeComponent();
            }
    
            private void ScrollViewer_OnViewChanged(object sender, ScrollViewerViewChangedEventArgs e)
            {
                OffsetTextBlock.Text =
                    ManipulationCaptureScrollViewer.VerticalOffset.ToString();
            }
        }
    }
    

    编辑*

    此外,在 Windows 8.1 中,您将获得 ManipulationModes.System,它与其他模式(但不支持缩放和旋转)结合使用应该允许您在 ScrollViewer 内处理操作。然后,一旦您希望其父 ScrollViewers 停止处理平移和缩放操作,您就可以在被操作元素上调用 CancelDirectManipulations()

    【讨论】:

    • 我最终制作了一个自定义控件,它包装了一个 FlipView,并实现了一个类似于 FlipView 默认的操作方案,以及一些在元素之间转换的动画。它工作得很好。不完全是 GridView,但它符合目的。感谢您的建议!
    • 是的。我会接受的。我只是想明确一点,接受是针对您的初步建议。它后面的代码要么在我头上,要么不适用于我的情况。再次感谢!
    • 此外,在 Windows 8.1 中,您将获得 ManipulationModes.System,它与其他模式相结合应该允许您处理 ScrollViewer 内部的操作。然后,一旦您希望其父 ScrollViewers 停止处理平移和缩放操作,您就可以在被操作元素上调用 CancelDirectManipulations()
    【解决方案2】:

    您可以在 xaml 中为 ManipulationMode 设置两个值。希望它会处理它。

    ManipulationMode="TranslateX,System"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-18
      • 2010-10-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多