Silverlight ScrollViewer Not Scrolling

enter code hereI have a ScrollViewer in Silverlight that is not scrolling vertically whenever I call the ScrollToVerticalOffset method from the code behind.

Basically, I have my View (UserControl) that contains the ScrollViewer. I invoke an action from my ViewModel that triggers an event in the View's code-behind that sets the VerticalOffset to a specific value.

First of all, I know this is very ugly. Ideally I wish that I could have an attachable property that I could bind to a property in my ViewModel, that, when set, would cause the VerticalOffset property (which I know is read-only) to be updated, and the ScrollViewer to scroll.

The ScrollViewer contains dynamic content. So, if the user is viewing content in the ScrollViewer, and scrolls half-way down, and then clicks on a button, new content is loaded into the ScrollViewer. The problem is that the ScrollViewer's vertical offset doesn't get reset, so the user has to scroll up to read the content. So, my solution was to be able to control the vertical offset from the ViewModel, and I have racked my brain and can't come up with a viable solution, so I am looking for someone to help, please.

By the way - I have included code from a class I put together for an attachable property. This property binds to a property in my ViewModel. When I set the property in the ViewModel, it correctly triggers the PropertyChanged callback method in this class, which then calls the ScrollToVerticalOffset method for the ScrollViewer, but the ScrollViewer still doesn't scroll.

public class ScrollViewerHelper { public static readonly DependencyProperty BindableOffsetProperty = DependencyProperty.RegisterAttached("BindableOffset", typeof(double), typeof(ScrollViewerHelper), new PropertyMetadata(OnBindableOffsetChanged)); public static double GetBindableOffset(DependencyObject d) { return (double)d.GetValue(BindableOffsetProperty); } public static void SetBindableOffset(DependencyObject d, double value) { d.SetValue(BindableOffsetProperty, value); } private static void OnBindableOffsetChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { ScrollViewer scrollViewer = d as ScrollViewer; if (scrollViewer != null) { scrollViewer.ScrollToVerticalOffset((double)e.NewValue); } } }

--------------Solutions-------------

This approach is a little bit funky in my opinion, as I think of both a ScrollViewer and a VerticalScrollOffset as "View" entities that should have very little (or nothing) to do with a ViewModel. It seems like this might be forcing MVVM a little too much, and creating a lot of extra work in creating an attached dependency property and basically trying to keep a bound Offset ViewModel property in sync with the readonly VerticalScrollOffset of the ScrollViewer.

I am not exactly sure of what you are trying to achieve, but it sounds like you are trying to scroll to a specified offset when some dynamic element is added to the underlying panel of your ScrollViewer. Personally, I would just want to handle this behavior with a little bit of code in my View and forget about tying it to the ViewModel.

One really nice way to do this type of thing in Silverlight 3 is with Blend behaviors. You write a little bit of behavior code in C# and then can attach it declaratively to an element in XAML. This keeps it reusable and out of your code-behind. Your project will have to reference the System.Windows.Interactivity DLL which is part of the Blend SKD.

Here's a simple example of a simple Blend behavior you could add to a ScrollViewer which scrolls to a specified offset whenever the size of the underlying content of the ScrollViewer changes:

public class ScrollToOffsetBehavior : Behavior<ScrollViewer>
{
private FrameworkElement contentElement = null;

public static readonly DependencyProperty OffsetProperty = DependencyProperty.Register(
"Offset",
typeof(double),
typeof(ScrollToOffsetBehavior),
new PropertyMetadata(0.0));

public double Offset
{
get { return (double)GetValue(OffsetProperty); }
set { SetValue(OffsetProperty, value); }
}

protected override void OnAttached()
{
base.OnAttached();

if (this.AssociatedObject != null)
{
this.AssociatedObject.Loaded += new RoutedEventHandler(AssociatedObject_Loaded);
}
}

protected override void OnDetaching()
{
base.OnDetaching();

if (this.contentElement != null)
{
this.contentElement.SizeChanged -= contentElement_SizeChanged;
}

if (this.AssociatedObject != null)
{
this.AssociatedObject.Loaded -= AssociatedObject_Loaded;
}
}

void AssociatedObject_Loaded(object sender, RoutedEventArgs e)
{
this.contentElement = this.AssociatedObject.Content as FrameworkElement;

if (this.contentElement != null)
{
this.contentElement.SizeChanged += new SizeChangedEventHandler(contentElement_SizeChanged);
}
}

void contentElement_SizeChanged(object sender, SizeChangedEventArgs e)
{
this.AssociatedObject.ScrollToVerticalOffset(this.Offset);
}
}

You could then apply this behavior to the ScrollViewer in XAML (and specify an offset of 0 to scroll back to the top):

<ScrollViewer>
<i:Interaction.Behaviors>
<local:ScrollToOffsetBehavior Offset="0"/>
</i:Interaction.Behaviors>
...Scroll Viewer Content...
</ScrollViewer>

This would be assuming that you always want to scroll to the offset whenever the content size changes. This may not be exactly what you are looking for, but it is one example of how something like this can be done in the view using a behavior.

Category:c# Time:2010-01-26 Views:0
Tags: c# silverlight

Related post

  • Silverlight Scrollviewer With Only Buttons 2008-12-17

    I am using a ScrollViewer as part of my Silverlight application. It has a horizontal orientation, and I would like it to appear such that only the scroll buttons appear, but not the scroll bar itself. Something like this crude ASCII rendering: ------

  • Nested UISCrollViews - Preventing Parent Scrollview from scrolling when zoomed on child 2009-06-18

    I have a UIScrollView with nested UIImageViews. Each imageview can zoom, but when I try to scroll the inner scrollview while zoomed on the image, the outer scrollview picks it up and switches imageviews. How can I prevent this from happening so that

  • Triggering on a ScrollViewer's scroll bar visibility (WPF) 2009-10-16

    Is it possible to create a Trigger that is triggered based on a ScrollViewer's scroll bar visibility? I have a ScrollViewer with its VerticalScrollBarVisibility set to Auto and I want to change some of the ScrollViewer's properties only when the scro

  • TabHost inside of a Scrollview: always scrolls down when a Tab is clicked 2010-01-06

    I have an Activity which has a Scrollview as the top level element. Inside there are some other Views and at some point there is a TabHost. You might get a better impression by looking at . The TabHost has three tabs, each with another Activity that

  • Why does TextView.setText cause the enclosing ScrollView to scroll? 2010-06-24

    I've got this odd problem which is happening on 1.6, 2.2, and a MyTouch 3G Slide (which is API #7, and listed as "2.1-Update1" in the Android Device Chooser). If anyone can explain what I'm doing wrong & how to fix it (or possibly confirm that th

  • How to stop ScrollViewer from Scrolling for Wp7 2011-02-09

    I have the following XAML : <ScrollViewer HorizontalAlignment="Left" Margin="3,226,0,0" Name="Scv" VerticalAlignment="Top" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto" > <Canvas Height="10

  • Prevent onFocusChanged firing when ScrollView is scrolled 2012-02-24

    I have a problem where I have several TextView objects in my Activity. More than can fit on the screen view so I have placed them inside a ScrollView. Now each TextView item is placed into a RelativeLayout before placing onto the ScrollView. Each Rel

  • Updating UITextView inside UIScrollView while scrolling makes the scrollView stop scrolling 2012-04-20

    I have a scrollView that contains 12 UIViews set as tiles. Each tile contains a textView. The scrollView has pagination enabled. So, you have 12 tiles per page and you scroll horizontally. I am updating the textView whenever I receive information fro

  • Main layout scrollview doesnt scroll vertically with viewpager tabs inside 2014-07-24

    I got an activity with a main scrollview with a viewPager inside. like the code below. The problem is that the main scrollview doesnt scroll vertically when the tab Fragment have more content than it shows on the screen, the viewpager and tabs(lib.:

  • How can I make the Silverlight ScrollViewer scroll to show a child control with focus? 2009-08-04

    I have a ScrollViewer which contains a Grid with multiple controls in it. The user can tab through the controls, but eventually they tab to a control that isn't in view - so they have to manully scroll to make the control visible again. Is there any

  • Silverlight 2.0 - scroll vertically, wrap horizontally 2008-12-01

    In silverlight 2.0. I have some content that i want to scroll vertically and wrap horizontally. In the controls I have a dock panel. The DockPanel's last child, which fills it, is a ScrollViewer <UserControl x:Class="MyProject.MyControl" xmlns="ht

  • Silverlight ScrollViewer takes focus when scrollbars are not visible 2010-08-19

    I'm finding that Silverlight's ScrollViewer will still take focus even when the scrollbars are not visible. Has anyone else seen this issue? Are there any workarounds that will prevent the ScrollViewer acting as a tabstop when the scrollbars are invi

  • Silverlight: Scrollviewer only appears when content overflows? 2010-12-10

    I'm using Silverlight 4. I have a UserControl whose LayoutRoot is wrapped in a ScrollViewer. I'd like the scroll bar to only appear if the LayoutRoot overflows the page. It is possible to do it automatically, or should I write code to detect if the c

  • Silverlight 4 Page Scroll on TAB hit 2011-03-03

    hey guys, I have a silverlight Navigation application where on one of my pages I have a form to be filled out by the end user, which has a couple of TextBoxes to be filled, the problem is that when I hit the TAB key to move to the next TextBox the cu

  • Silverlight maintain ListBox scroll position 2011-03-15

    I have a ListBox control that I use an ObservableCollection to add items to in my ViewModel however I notice the ListBox doesn't maintain the scroll position as I would expect. I'm new to Silverlight and opted to go with MVVM but I can't figure out t

  • Silverlight ScrollViewer 2011-04-08

    I have a ContentControl wrapped in a ScrollViewer but for some reason I cant work out even though the content that I place within the ContentControl is bigger than the visible space the scrollbars do not get enabled. The verticalscrollbarvisibilty is

  • silverlight scrollviewer and canvas with h/w set. Cant get vertical bar to hide even when lots of room 2011-08-11

    Silverlight project with tabcontrol hosting canvas objects. I want to wrap my entire tabcontrol with a scrollviewer so that on a short display you can scroll vertically to see the whole area on my canvas. When I run my project my scrollviewer vertica

  • Silverlight 4 RichTextBox scroll to bottom 2011-08-25

    I am writing a chat application with Silverlight. I am adding the content dynamically to the rich text box and I need to scroll it down to show the last messages. But there is no way to do that. All documentation/codes that I have found on internet a

  • Silverlight ScrollViewer not updating after zoom 2012-02-13

    I have a Silverlight grid with a bunch of content in it (rectangles, textBlocks, etc.) which represents content in a room. Because it gets pretty complex, I decided I needed an ability to "zoom-in" on the grid. I found some good code to do that, but

Copyright (C) pcaskme.com, All Rights Reserved.

processed in 0.531 (s). 13 q(s)