平移自定义控件时不会进行滚动。我有一个来自Panel的自定义控件,我把这个控件放在ScrollViewer
里面。我已将自定义控件的ManipulationMode
设置为"全部"。并使用ManipulationDelta
中的VericalOffset
方法触发HorizontalOffSet
并更改ChangeView
和ScrollViewer
。根据我的平移为我的面板设置了VerticalOffSet和HorizontalOffSet,但是在视图中没有变化,ScrollViewer
保存了Panel。
我已经厌倦了许多方法,但问题仍未解决。触发ViewChanging
并设置VerticalOffSet
和HorizontalOffSet
。触发ManipulationDelta
的{{1}}并设置ScrollViewer
和VerticalOffSet
。触发HorizontalOffSet
,ManipulationStaring
和ManipulationDelta
,为CustomPanel和ManipulationCompleted
设置VerticalOffSet
和HorizontalOffSet
。
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
。
对此有何建议?
答案 0 :(得分:0)
平移自定义控件时不会滚动。
如果我理解正确,我认为问题是VerticalOffset - verticalOffset
和HorizontalOffset - 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
触发时所见,我增加了HorizontalOffset
和VerticalOffset
。
以下是结果: