0

I am using WPF and MVVM. I have list that contains many items.I have bound that list to List box. Every item in list execute sequentially, after completion of first item it will automatically execute second item. My problem is that scrollBar is not moving automatically with respective item in list.I want to move scroll bar with respective item in list.

This is my property:

private List<TestItem> testItemsList;
public List<TestItem> TestItemsList
{ get { return testItemsList; }
  set { SetProperty(ref testItemsList, value); } }

This is my xaml file data binding:

 <ListBox x:Name="listBox" Margin="6,0.938,2.5,4.34" ItemsSource="{Binding StationProperty.TestItemsList}">
             <ListBox.ItemTemplate>
              <DataTemplate>
                    <Grid Margin="1" x:Name="BackgroundGrid">
                        <TextBlock Grid.Column="1" AllowDrop="True"  Text="{Binding TestItemName}" />
                    </Grid>
             </DataTemplate>
            </ListBox.ItemTemplate>
     </ListBox>
  • You'll need to move the scroll bar programatically. see https://stackoverflow.com/questions/2006729/how-can-i-have-a-listbox-auto-scroll-when-a-new-item-is-added – d.moncada Mar 23 '17 at 03:31
  • Possible duplicate of [How can I have a ListBox auto-scroll when a new item is added?](http://stackoverflow.com/questions/2006729/how-can-i-have-a-listbox-auto-scroll-when-a-new-item-is-added) – d.moncada Mar 23 '17 at 03:31

1 Answers1

0

use below code

public static class ListViewItemBehavior
    {
        public static readonly DependencyProperty AutoScrollToCurrentItemProperty =
            DependencyProperty.RegisterAttached("AutoScrollToSelectedItem",
                typeof(bool), typeof(ListViewItemBehavior),
                new UIPropertyMetadata(default(bool), OnAutoScrollToCurrentItemChanged));

        public static bool GetAutoScrollToCurrentItem(DependencyObject obj)
        {
            return (bool)obj.GetValue(AutoScrollToCurrentItemProperty);
        }

        public static void OnAutoScrollToCurrentItemChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            var listView = obj as ListBox;
            if (listView == null) return;

            var newValue = (bool)e.NewValue;
            if (newValue)
                listView.SelectionChanged += listviewSelectionChanged;
            else
                listView.SelectionChanged -= listviewSelectionChanged;
        }

        static void listviewSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var listview = sender as ListView;
            if (listview == null || listview.SelectedItem == null || listview.Items == null) return;

            listview.Items.MoveCurrentTo(listview.SelectedItem);
            listview.ScrollIntoView(listview.SelectedItem);
        }

        public static void SetAutoScrollToCurrentItem(DependencyObject obj, bool value)
        {
            obj.SetValue(AutoScrollToCurrentItemProperty, value);
        }
    }

Add Reference in view:

 xmlns:listviewHelper="clr-namespace:XXXX.DependencyObjects"

In listview use like below:

 <ListView listviewHelper:ListViewItemBehavior.AutoScrollToCurrentItem="True">

Above code is tested it works surely..please try.

stylishCoder
  • 385
  • 1
  • 19