Trying to scroll scrollviewer programmatically

I am developing a WPF application in which I have a ScrollViewer with a grid (16 ColumnDefinitions all auto width) with an image assigned to each column. The effect I want to create is when the mouse enters the area to the left of the ScrollViewer, the images will scroll to the left ... and when the mouse enters the area to the right of the ScrollViewer, they scroll to the right. Scroll bars will be hidden. The areas on the left and right are defined by two rectangles with the mouseEnter and mouseLeave events. My code uses a timer to program the ScrollViewer to the left.

Here is my code ...

Imports System Imports System.IO Imports System.Net Imports System.Windows Imports System.Windows.Controls Imports System.Windows.Data Imports System.Windows.Media Imports System.Windows.Media.Animation Imports System.Windows.Navigation Partial Public Class Crime Dim ScrollLeft As Boolean = True Dim atimer As New System.Timers.Timer() Public Sub New() MyBase.New() Me.InitializeComponent() ' Insert code required on object creation below this point. ' Hook up the Elapsed event for the timer. AddHandler atimer.Elapsed, AddressOf Me.timer_Tick atimer.Interval = 100 atimer.Enabled = True End Sub 'CODE TO SCROLL SCROLLVIEWER PROGRAMATICALLY Private Sub timer_Tick(sender As Object, e As EventArgs) If ScrollLeft Then svImages.ScrollToHorizontalOffset(svImages.HorizontalOffset - 1) Else svImages.ScrollToHorizontalOffset(svImages.HorizontalOffset + 1) End If End Sub Private Sub Left_MouseEnter(sender As Object, e As MouseEventArgs) ScrollLeft = True atimer.Start() End Sub Private Sub Right_MouseEnter(sender As Object, e As MouseEventArgs) ScrollLeft = False atimer.Start() End Sub Private Sub Left_MouseLeave(sender As Object, e As MouseEventArgs) atimer.Stop() ScrollLeft = True End Sub Private Sub Right_MouseLeave(sender As Object, e As MouseEventArgs) atimer.Stop() End Sub End Class 

If I run this from Expressions Blend, the project builds OK and displays, but the scroll action does not work.

If I run this from Visual Studio, I get an error message with the line ...

 svImages.ScrollToHorizontalOffset(svImages.HorizontalOffset - 0.1) 

The InvalidOperationException was not processed by the user code ... The calling thread cannot access this object because another thread belongs to it. I get this error before the window even loads.

Where am I wrong?

+4
source share
1 answer

The error "The calling thread cannot access this object because another thread belongs to it." is standard on WinForms and WPF controls.

In WPF and WinForms, windows are displayed on the screen using one specific thread, usually called a user interface thread. Each update / change action regarding the controls must take place in this thread in order to be successful.

The usual way to work with WinForms is to create a special delegate and call Control.Invoke, as shown in this link .

As in WPF, the same effect is achieved using Dispatcher . Your code should look like this:

 this.Dispatcher.Invoke( () => svImages.ScrollToHorizontalOffset(svImages.HorizontalOffset - 0.1)); 

UPDATE:

I have the following code to work in VB.NET:

 Private Delegate Sub ScrollDelegate(ByVal offset As Double) Private Sub ScrollLeft(ByVal offset As Double) svImages.ScrollToHorizontalOffset(svImages.HorizontalOffset + offset) End Sub // ... calling from background thread Dim slt As ScrollDelegate slt = New ScrollDelegate(AddressOf ScrollLeft) Me.Dispatcher.Invoke(slt) 

Update 2

The code has changed for the question.

 Dim ScrollLeft As Boolean = True Dim atimer As New System.Timers.Timer() Dim scrollMethod As ScrollDelegate Private Delegate Sub ScrollDelegate(ByVal offset As Double) // ... Me.InitializeComponent() slt = New ScrollDelegate(AddressOf DoScroll) // ... Private Sub timer_Tick(sender As Object, e As EventArgs) If ScrollLeft Then Me.Dispatcher.Invoke(slt, -1) Else Me.Dispatcher.Invoke(slt, 1) End If End Sub // ... Private Sub DoScroll(ByVal offset As Double) svImages.ScrollToHorizontalOffset(svImages.HorizontalOffset + offset) End Sub 
+8
source

Source: https://habr.com/ru/post/1403162/


All Articles