Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8470197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T16:27:03+00:00 2026-06-10T16:27:03+00:00

I have an attached behavior to an ItemsControl that scrolls down to the bottom

  • 0

I have an attached behavior to an ItemsControl that scrolls down to the bottom whenever a new item is added. Since I am working on a chat type program, I don’t want it to scroll if the user has the scrollbar anywhere other than the very bottom as that would be very annoying otherwise(Some chat programs do this and it’s awful).

How do I accomplish this? I don’t know how to access the wrapping ScrollViewer, or otherwise figure out if I need to bring it into view or not.

This is the behavior class that I actually got from someone on StackOverflow. I’m still learning about behaviors myself.

public class ScrollOnNewItem : Behavior<ItemsControl>
{
    protected override void OnAttached()
    {
        AssociatedObject.Loaded += OnLoaded;
        AssociatedObject.Unloaded += OnUnLoaded;
    }

    protected override void OnDetaching()
    {
        AssociatedObject.Loaded -= OnLoaded;
        AssociatedObject.Unloaded -= OnUnLoaded;
    }

    private void OnLoaded(object sender, RoutedEventArgs e)
    {
        var incc = AssociatedObject.ItemsSource as INotifyCollectionChanged;
        if (incc == null) return;

        incc.CollectionChanged += OnCollectionChanged;
    }

    private void OnUnLoaded(object sender, RoutedEventArgs e)
    {
        var incc = AssociatedObject.ItemsSource as INotifyCollectionChanged;
        if (incc == null) return;

        incc.CollectionChanged -= OnCollectionChanged;
    }

    private void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        if (e.Action == NotifyCollectionChangedAction.Add)
        {
            int count = AssociatedObject.Items.Count;
            if (count == 0)
                return;

            var item = AssociatedObject.Items[count - 1];

            var frameworkElement = AssociatedObject.ItemContainerGenerator.ContainerFromItem(item) as FrameworkElement;
            if (frameworkElement == null) return;

            frameworkElement.BringIntoView();
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T16:27:04+00:00Added an answer on June 10, 2026 at 4:27 pm

    Okay, here’s the answer I came up with for myself.

    I figured out that there is a GetSelfAndAncestors method for dependency objects. Using that, I am able to get the ScrollViewer ancestor(if there is one) of my AssociatedObject(the ItemsControl) and manipulate it with that.

    So I added this field to my behavior

    private ScrollViewer scrollViewer;
    private bool isScrollDownEnabled;
    

    And in the OnLoaded event handler I assigned it with the following code

    scrollViewer = AssociatedObject.GetSelfAndAncestors().Where(a => a.GetType().Equals(typeof(ScrollViewer))).FirstOrDefault() as ScrollViewer;
    

    And in the OnCollectionChanged event handler, I went ahead and wrapped all the logic in an if statement as follows

            if (scrollViewer != null)
            {
                isScrollDownEnabled = scrollViewer.ScrollableHeight > 0 && scrollViewer.VerticalOffset + scrollViewer.ViewportHeight < scrollViewer.ExtentHeight;
    
                if (e.Action == NotifyCollectionChangedAction.Add && !isScrollDownEnabled)
                {
                     // Do stuff
                }
            }
    

    So all together, the code looks like the following

    public class ScrollOnNewItem : Behavior<ItemsControl>
    {
        private ScrollViewer scrollViewer;
        private bool isScrollDownEnabled;
    
        protected override void OnAttached()
        {
            AssociatedObject.Loaded += OnLoaded;
            AssociatedObject.Unloaded += OnUnLoaded;
        }
    
        protected override void OnDetaching()
        {
            AssociatedObject.Loaded -= OnLoaded;
            AssociatedObject.Unloaded -= OnUnLoaded;
        }
    
        private void OnLoaded(object sender, RoutedEventArgs e)
        {
            var incc = AssociatedObject.ItemsSource as INotifyCollectionChanged;
            if (incc == null) return;
    
            incc.CollectionChanged += OnCollectionChanged;
            scrollViewer = AssociatedObject.GetSelfAndAncestors().Where(a => a.GetType().Equals(typeof(ScrollViewer))).FirstOrDefault() as ScrollViewer;
        }
    
        private void OnUnLoaded(object sender, RoutedEventArgs e)
        {
            var incc = AssociatedObject.ItemsSource as INotifyCollectionChanged;
            if (incc == null) return;
    
            incc.CollectionChanged -= OnCollectionChanged;
        }
    
        private void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (scrollViewer != null)
            {
                isScrollDownEnabled = scrollViewer.ScrollableHeight > 0 && scrollViewer.VerticalOffset + scrollViewer.ViewportHeight < scrollViewer.ExtentHeight;
    
                if (e.Action == NotifyCollectionChangedAction.Add && !isScrollDownEnabled)
                {
                    int count = AssociatedObject.Items.Count;
                    if (count == 0)
                        return;
    
                    var item = AssociatedObject.Items[count - 1];
    
                    var frameworkElement = AssociatedObject.ItemContainerGenerator.ContainerFromItem(item) as FrameworkElement;
                    if (frameworkElement == null) return;
    
                    frameworkElement.BringIntoView();
                }
            }
        }
    }
    

    As asked in the comments, to use a behavior, I just need to add a new xmlns to my xaml file of the area in code that contains my behavior.

                 xmlns:behaviors="clr-namespace:Infrastructure.Behaviors;assembly=Infrastructure"
    

    Then on the control I just add on the behavior.

        <ScrollViewer VerticalScrollBarVisibility="Auto">
            <ItemsControl Name="Blah" ItemsSource="{Binding Messages}" ItemTemplate="{StaticResource MessageTemplate}">
                <i:Interaction.Behaviors>
                    <behaviors:ScrollOnNewItem />
                </i:Interaction.Behaviors>
            </ItemsControl>
        </ScrollViewer>
    

    The i class is just the interactivity namespace. xmlns:i=”clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity”

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Behavior attached to a Model that should behave differently depending on
I have implemented a crypt behavior class that can be attached to a AR
I have a working attached behavior which I would like to add a DP
I'm trying to unit test a private method that I have attached to my
I have an attached behavior defined thusly,.. public static class FileBrowserBehaviour { public static
My goal is to create a reusable Attached Behavior for a FlowDocumentScrollViewer, so that
I have found that the following behavior in Chrome, not in Firefox. I have
I have some GLSL shaders that are compiling correctly, and being successfully attached to
I have a gui that needs to be updated from a hardware device attached
I have attached the translate behavior at a model and inside the controller in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.