问题描述
限时送ChatGPT账号..我有一个带有列表框的滚动查看器.我想让滚动查看器在每次计时器滴答时使用 ScrollToHorizontalOffset 自动滚动(每 1 秒滚动 100).但是它不起作用,计时器工作正常,但滚动查看器不会移动.这是我的代码,请帮忙!
I have a scrollviewer that has a listbox in it. I want to have the scrollviewer automatically scroll by using ScrollToHorizontalOffset each time the timer tick (scroll 100 each 1 second). However it won't work, the timer work fine but the scrollviewer just won't move. Here is my code, please help!
DispatcherTimer timer = new DispatcherTimer();
double current = 0;
// Constructor
public MainPage()
{
InitializeComponent();
timer = new DispatcherTimer();
this.imagesScrollview.Loaded += new RoutedEventHandler(imagesScrollview_Loaded);
timer.Interval = TimeSpan.FromSeconds(1);
timer.Tick += new EventHandler(timer_Tick);
timer.Start();
}
void timer_Tick(object sender, EventArgs e)
{
// ScrollViewer sv = new ScrollViewer();
imagesScrollview.InvalidateScrollInfo();
imagesScrollview.ScrollToHorizontalOffset(current);
imagesScrollview.UpdateLayout();
current = current + 100;
textBlock2.Text = current.ToString();
}
和我的滚动查看器:
<ScrollViewer HorizontalScrollBarVisibility="Auto" Margin="8,563,8,2" Width="auto" x:Name="imagesScrollview" Opacity="1" Background="#FF3ED216" Grid.Row="1" RenderTransformOrigin="0.5,0.5" Loaded="imagesScrollview_Loaded">
<ScrollViewer.RenderTransform>
<CompositeTransform/>
</ScrollViewer.RenderTransform>
<ListBox x:Name="listBox" Width="Auto" Height="Auto" Background="#FF3ED216" ManipulationCompleted="listBox_ManipulationCompleted">
<ListBox.RenderTransform>
<CompositeTransform/>
</ListBox.RenderTransform>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal">
<StackPanel.RenderTransform>
<TranslateTransform X="0" />
</StackPanel.RenderTransform>
</StackPanel>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="15,0">
<Image x:Name="imageAV" Source="{Binding avlink}" Height="80" Width="80" Stretch="UniformToFill" MouseLeftButtonUp="imageAV_MouseLeftButtonUp" ImageFailed="imageAV_ImageFailed" />
<StackPanel Orientation="Vertical" Margin="10,0,0,0" MouseLeftButtonUp="StackPanel_MouseLeftButtonUp">
<TextBlock Text="{Binding nickname}" Width="120"/>
<TextBlock Text="{Binding track}" FontWeight="Bold" Width="120"/>
<TextBlock Text="{Binding artist}" Width="120"/>
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</ScrollViewer>
推荐答案
与其使用外部的 ScrollViewer,不如使用 ListBox 内部的那个.
Rather than use an external ScrollViewer, you should use the the one inside the ListBox.
假设存在一个名为MainListBox"的列表框,这将使列表顶部的项目每秒前进一个:
Assuming the existence of a ListBox called "MainListBox" this will advance the item at the top of the list by one each second:
var dt = new DispatcherTimer { Interval = TimeSpan.FromSeconds(1) };
dt.Tick += (s, args) =>
{
var count = VisualTreeHelper.GetChildrenCount(this.MainListBox);
for (var idx = 0; idx < count; idx++)
{
var child = VisualTreeHelper.GetChild(this.MainListBox, idx);
if (child is ScrollViewer)
{
var sv = child as ScrollViewer;
sv.ScrollToVerticalOffset(sv.VerticalOffset + 1);
break;
}
}
};
dt.Start();
是的,它可以更好,但它证明这是可能的.
这篇关于为什么我的滚动查看器在 WP7 中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论