,因为我需要在我的WP7项目之一,显示长文本串,我说Alex Yakhnin's ScrollableTextBlock控制它。编程滚动ScrollableTextBlock控制
它按预期工作,但我需要在某些用户交互时自动滚动文本块,但该控件不支持ScrollToVerticalOffset开箱即用,我不知道该如何去做。
我想删除元素,并添加新的人会做,但我认为这是做得有点过分了这样一个简单的任务。
,因为我需要在我的WP7项目之一,显示长文本串,我说Alex Yakhnin's ScrollableTextBlock控制它。编程滚动ScrollableTextBlock控制
它按预期工作,但我需要在某些用户交互时自动滚动文本块,但该控件不支持ScrollToVerticalOffset开箱即用,我不知道该如何去做。
我想删除元素,并添加新的人会做,但我认为这是做得有点过分了这样一个简单的任务。
一类丑陋的解决办法是到负载事件添加到ScrollViewer中。当事件触发时,获取发件人(这是ScrollViewer,呵呵!)并将其存储在类级变量中。通过这种方式,您可以使用其他方法访问它,并且可以使用ScrollToVerticalOffset方法。
XAML
<PhoneApp1:ScrollableTextBlock x:Name="TextBlockOne" Margin="0,0,-12,0">
<PhoneApp1:ScrollableTextBlock.Style>
<Style TargetType="PhoneApp1:ScrollableTextBlock" >
<Setter Property="Foreground" Value="{StaticResource PhoneForegroundBrush}"/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="FontSize" Value="{StaticResource PhoneFontSizeMedium}"/>
<Setter Property="Padding" Value="0"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="PhoneApp1:ScrollableTextBlock">
<ScrollViewer Foreground="{TemplateBinding Foreground}"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Padding="{TemplateBinding Padding}"
Loaded="ScrollViewer_Loaded">
<StackPanel Orientation="Vertical" x:Name="StackPanel" />
</ScrollViewer>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</PhoneApp1:ScrollableTextBlock.Style>
</PhoneApp1:ScrollableTextBlock>
C#
private ScrollViewer _scroller;
private void ScrollViewer_Loaded(object sender, System.Windows.RoutedEventArgs e)
{
_scroller = sender as ScrollViewer;
}
private void SomeMethod()
{
_scroller.ScrollToVerticalOffset(200d);
}
我知道这可能不是最好的解决方案,但它的工作原理。希望这可以帮助!
当您已经获得ScrollViewer
实例(使用FindName
或其他)时,则需要在scroll offset
上创建依赖项属性以为其设置动画效果。 This可能会帮助你。
事实上,可能不是最好的方式,但仍然奏效。 – 2012-03-12 12:52:20