在UWP

时间:2016-08-05 08:35:43

标签: uwp panel panning

平移自定义控件时不会进行滚动。我有一个来自Panel的自定义控件,我把这个控件放在ScrollViewer里面。我已将自定义控件的ManipulationMode设置为"全部"。并使用ManipulationDelta中的VericalOffset方法触发HorizontalOffSet并更改ChangeViewScrollViewer。根据我的平移为我的面板设置了VerticalOffSet和Horizo​​ntalOffSet,但是在视图中没有变化,ScrollViewer保存了Panel。

我已经厌倦了许多方法,但问题仍未解决。触发ViewChanging并设置VerticalOffSetHorizontalOffSet。触发ManipulationDelta的{​​{1}}并设置ScrollViewerVerticalOffSet。触发HorizontalOffSetManipulationStaringManipulationDelta,为CustomPanel和ManipulationCompleted设置VerticalOffSetHorizontalOffSet

ScrollViewer

注意:我的自定义网格(CustomPanel)中有一些固定行,而滚动没有设置<Grid> <ScrollViewer x:Name="scrollViewer" HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Visible" HorizontalAlignment="Left" VerticalAlignment="Top"> <local:CustomPanel x:Name="customPanel" Height="800" Width="900" ManipulationMode="All"/> </ScrollViewer> </Grid> private void OnContainerOnManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e) { if (scrollViewer!= null) { if (scrollViewer.HorizontalScrollMode == ScrollMode.Disabled && scrollViewer.VerticalScrollMode == ScrollMode.Disabled) return; var verticalOffset = e.Delta.Translation.Y; var horizontalOffset = e.Delta.Translation.X; this.scrollViewer.ChangeView(null, VerticalOffset - verticalOffset, null, true); this.scrollViewer.ChangeView(HorizontalOffset - horizontalOffset, null, null, true);); e.Handled = true; } } 固定行闪烁,所以只触发了ManipulationMode

对此有何建议?

1 个答案:

答案 0 :(得分:0)

  

平移自定义控件时不会滚动。

如果我理解正确,我认为问题是VerticalOffset - verticalOffsetHorizontalOffset - horizontalOffset的值不会发生变化。

操作通常与RenderTransform合作。如果您要平移您的UIElement,则需要向TranslateTransform提供CustomPanel

我做了一个演示并修改了你的代码,如下所示:

public sealed partial class MainPage : Page
{
    private TranslateTransform dragTranslation;
    private double HorizontalOffset;
    private double VerticalOffset;
    public MainPage()
    {
        this.InitializeComponent();
        customPanel.ManipulationDelta += CustomPanel_ManipulationDelta;
        dragTranslation = new TranslateTransform();
        //give a translate transform to customPanel.
        customPanel.RenderTransform = this.dragTranslation;
    }

    private void CustomPanel_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
    {
        if (scrollViewer != null)
        {
            if (scrollViewer.HorizontalScrollMode == ScrollMode.Disabled && scrollViewer.VerticalScrollMode == ScrollMode.Disabled)
                return;
            var verticalOffset = e.Delta.Translation.Y;
            var horizontalOffset = e.Delta.Translation.X;
            this.dragTranslation.X += horizontalOffset;
            this.dragTranslation.Y += verticalOffset;
            HorizontalOffset += horizontalOffset;//Here dynamically increase the HorizontalOffset;
            VerticalOffset += verticalOffset;//Here dynamically increase the VerticalOffset;
            this.scrollViewer.ChangeView(null,VerticalOffset, null, true);
            this.scrollViewer.ChangeView(HorizontalOffset, null, null, true);
            e.Handled = true;
        }
    }
}

和XAML:

<ScrollViewer x:Name="scrollViewer" VerticalAlignment="Center"
                  HorizontalScrollBarVisibility="Visible" 
                  VerticalScrollBarVisibility="Visible"
                  HorizontalAlignment="Left" 
                  >
        <local:CustomPanel x:Name="customPanel" Width="800" Height="800" 
                           ManipulationMode="All" VerticalAlignment="Center">
            <TextBlock> Panning Me</TextBlock>
        </local:CustomPanel>
</ScrollViewer>

正如您每次CustomPanel_ManipulationDelta触发时所见,我增加了HorizontalOffsetVerticalOffset

以下是结果:

enter image description here