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();
}
and my scrollviewer:
<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>
Rather than use an external ScrollViewer, you should use the the one inside the ListBox.
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();
Yes, it could be much nicer but it proves it's possible.
ListBox by default has it's own scroll if you want to wrap it in a ScrollViewer you need to disable it's scroll:
<ListBox ScrollViewer.VerticalScrollBarVisibility="Disabled"
the ScrollToHorizontalOffset and ScrollToVerticalOffset doesn't work in Windows phone development tool beta, the bug was fixed in beta2 version.... My code above works fine in the beta2 version. **ing stuck at this for 2 days!
精彩评论