Trivial ListBox/ListView AutoscrollHello,<br> <br>I created a simple ListBox and bound it to some ObservableCollection and I'd like the listbox to autoscroll when I'm adding item to the source collection. Since I'm really into WPF UI/data separation, my data source object doesn't know anything about the ListBox (so it cannot call ListBox.ScrollIntoView on each item add).<br><br>The easiest way I could come to seems to be extremally tricky for such a trivial task. Something like this:<br> <p style="margin:0in 0in 0pt"> </p> <div style="border-right:#7f9db9 1px solid;border-top:#7f9db9 1px solid;font-size:11px;overflow:auto;border-left:#7f9db9 1px solid;line-height:100%! important;border-bottom:#7f9db9 1px solid;font-family:Courier New;background-color:white"><br> <table style="border-top-width:0px;border-left-width:0px;margin:2px 0px;width:99%;border-bottom:#eee 0px solid;border-collapse:collapse;background-color:#fff;border-right-width:0px" cellspacing=0 cellpadding=0> <colgroup> <col style="padding-left:10px;font-size:11px;border-bottom:#f7f7f7 1px solid;font-family:Courier New;white-space:nowrap"> <tbody> <tr> <td><font style="font-size:11px">&lt;ListBox Name=</font><font style="color:blue">&quot;lb&quot;</font><font style="font-size:11px"> ItemsSource=</font><font style="color:blue">&quot;{Binding Path=Data}&quot;</font><font style="font-size:11px"> VirtualizingStackPanel.IsVirtualizing=</font><font style="color:blue">&quot;False&quot;</font><font style="font-size:11px">/&gt;  </font></td></tr> <tr> <td style="background-color:#f7f7f7">lb.IsSynchronizedWithCurrentItem = </font><font style="color:blue">true</font><font style="font-size:11px">;  </font></td></tr> <tr> <td>lb.SelectionChanged += (s, e1) =&gt; { lb.ScrollIntoView(lb.Items[lb.Items.Count-1]); };  </td></tr> <tr> <td style="background-color:#f7f7f7"> </td></tr> <tr> <td></font><font style="color:green">// on every Add to the source collection: </font><font style="font-size:11px"> </font></td></tr> <tr> <td style="background-color:#f7f7f7">..Data.Add(“blablabla”);  </td></tr> <tr> <td>CollectionViewSource.GetDefaultView(..Data).MoveCurrentToLast();  <br></td></tr> <tr> <td style="background-color:#f7f7f7"> </td></tr></tbody></table></div><br><br><font style="font-size:12px">Another way would be to &quot;listen&quot; to ItemSource changes for my ListBox/ListView but this is as tricky as the sample above (and would also bring UI/data separation because this way ListBox should know some stuff about its data source)<br><br>There are several other ways to achieve the same thing, but they are even more tricky (e.g. sit on ItemContainerGenerator events, or find ScrollViewer in the visual tree and play with its scroll offset etc.).<br><br>Am I missing something or there's really no straightforward way to do such a common task in WPF?<br><br>Thanks,<br>  Boris.<br></font>© 2009 Microsoft Corporation. All rights reserved.Wed, 29 Apr 2009 02:51:56 Z0f524459-b14e-4f9a-8264-267953418a2dhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/0f524459-b14e-4f9a-8264-267953418a2d#0f524459-b14e-4f9a-8264-267953418a2dhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/0f524459-b14e-4f9a-8264-267953418a2d#0f524459-b14e-4f9a-8264-267953418a2dBorka_http://social.msdn.microsoft.com/Profile/en-US/?user=Borka_Trivial ListBox/ListView AutoscrollHello,<br> <br>I created a simple ListBox and bound it to some ObservableCollection and I'd like the listbox to autoscroll when I'm adding item to the source collection. Since I'm really into WPF UI/data separation, my data source object doesn't know anything about the ListBox (so it cannot call ListBox.ScrollIntoView on each item add).<br><br>The easiest way I could come to seems to be extremally tricky for such a trivial task. Something like this:<br> <p style="margin:0in 0in 0pt"> </p> <div style="border-right:#7f9db9 1px solid;border-top:#7f9db9 1px solid;font-size:11px;overflow:auto;border-left:#7f9db9 1px solid;line-height:100%! important;border-bottom:#7f9db9 1px solid;font-family:Courier New;background-color:white"><br> <table style="border-top-width:0px;border-left-width:0px;margin:2px 0px;width:99%;border-bottom:#eee 0px solid;border-collapse:collapse;background-color:#fff;border-right-width:0px" cellspacing=0 cellpadding=0> <colgroup> <col style="padding-left:10px;font-size:11px;border-bottom:#f7f7f7 1px solid;font-family:Courier New;white-space:nowrap"> <tbody> <tr> <td><font style="font-size:11px">&lt;ListBox Name=</font><font style="color:blue">&quot;lb&quot;</font><font style="font-size:11px"> ItemsSource=</font><font style="color:blue">&quot;{Binding Path=Data}&quot;</font><font style="font-size:11px"> VirtualizingStackPanel.IsVirtualizing=</font><font style="color:blue">&quot;False&quot;</font><font style="font-size:11px">/&gt;  </font></td></tr> <tr> <td style="background-color:#f7f7f7">lb.IsSynchronizedWithCurrentItem = </font><font style="color:blue">true</font><font style="font-size:11px">;  </font></td></tr> <tr> <td>lb.SelectionChanged += (s, e1) =&gt; { lb.ScrollIntoView(lb.Items[lb.Items.Count-1]); };  </td></tr> <tr> <td style="background-color:#f7f7f7"> </td></tr> <tr> <td></font><font style="color:green">// on every Add to the source collection: </font><font style="font-size:11px"> </font></td></tr> <tr> <td style="background-color:#f7f7f7">..Data.Add(“blablabla”);  </td></tr> <tr> <td>CollectionViewSource.GetDefaultView(..Data).MoveCurrentToLast();  <br></td></tr> <tr> <td style="background-color:#f7f7f7"> </td></tr></tbody></table></div><br><br><font style="font-size:12px">Another way would be to &quot;listen&quot; to ItemSource changes for my ListBox/ListView but this is as tricky as the sample above (and would also bring UI/data separation because this way ListBox should know some stuff about its data source)<br><br>There are several other ways to achieve the same thing, but they are even more tricky (e.g. sit on ItemContainerGenerator events, or find ScrollViewer in the visual tree and play with its scroll offset etc.).<br><br>Am I missing something or there's really no straightforward way to do such a common task in WPF?<br><br>Thanks,<br>  Boris.<br></font>Sun, 09 Nov 2008 18:26:30 Z2008-11-09T18:27:15Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/0f524459-b14e-4f9a-8264-267953418a2d#f4ac54f1-c860-4e5f-8baa-8c7dd01c4f1ahttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/0f524459-b14e-4f9a-8264-267953418a2d#f4ac54f1-c860-4e5f-8baa-8c7dd01c4f1aAlexander Yudakovhttp://social.msdn.microsoft.com/Profile/en-US/?user=Alexander%20YudakovTrivial ListBox/ListView Autoscroll<p>Interesting task.  Well, let`s solve it:  we keep UI/data separation principle and move all the implementation stuff into the separate code file.<br><br>In the example window XAML we just declare: ListBox have to auto-scroll:<font color="#0000ff" size=2></p> <div style="border-right:#7f9db9 1px solid;border-top:#7f9db9 1px solid;font-size:11px;overflow:auto;border-left:#7f9db9 1px solid;line-height:100%! important;border-bottom:#7f9db9 1px solid;font-family:Courier New;background-color:white"> <table style="border-top-width:0px;border-left-width:0px;margin:2px 0px;width:99%;border-bottom:#eee 0px solid;border-collapse:collapse;background-color:#fff;border-right-width:0px" cellspacing=0 cellpadding=0> <colgroup> <col style="padding-left:10px;font-size:11px;border-bottom:#f7f7f7 1px solid;font-family:Courier New;white-space:nowrap"> <tbody> <tr> <td><font style="font-size:11px"></font><font style="font-size:12px"><font style="font-size:12px;color:blue"><br><font style="font-size:12px">&lt;</font></font><font style="font-size:12px"><font style="font-size:12px"><font style="font-size:12px"><font style="font-size:12px" color="#990000">Window</font> <font style="font-size:12px" color="#ff0000">x</font>:</font><font style="font-size:12px;color:red">Class</font><font style="font-size:12px">=</font><font style="font-size:12px;color:blue">&quot;ListBoxAutoScrollTest.Window1&quot;</font><font style="font-size:12px"> </font></font></font></font></td></tr> <tr> <td style="background-color:#f7f7f7"><font style="font-size:12px"><font style="font-size:12px"><font style="font-size:12px">        </font><font style="font-size:12px;color:red">xmlns</font><font style="font-size:12px">=</font><font style="font-size:12px;color:blue">&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;</font><font style="font-size:12px"> </font></font></font></font></td></tr> <tr> <td><font style="font-size:12px"><font style="font-size:12px"><font style="font-size:12px">        <font style="font-size:12px" color="#ff0000">xmlns</font>:</font><font style="font-size:12px;color:red">x</font><font style="font-size:12px">=</font><font style="font-size:12px;color:blue">&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;</font><font style="font-size:12px"> </font></font></font></font></td></tr> <tr> <td style="background-color:#f7f7f7"><font style="font-size:12px"><font style="font-size:12px"><font style="font-size:12px">        <font style="font-size:12px;background-color:#cccccc"><font style="font-size:12px" color="#ff0000">xmlns</font>:<font style="font-size:12px;color:red">w</font><font style="font-size:12px">=</font><font style="font-size:12px;color:blue">&quot;clr-namespace:System.Windows.Workarounds&quot;</font></font></font><font style="font-size:12px"> </font></font></font></font></td></tr> <tr> <td><font style="font-size:12px"><font style="font-size:12px"><font style="font-size:12px">        </font><font style="font-size:12px;color:red">Title</font><font style="font-size:12px">=</font><font style="font-size:12px;color:blue">&quot;Window1&quot;</font><font style="font-size:12px"> </font><font style="font-size:12px;color:red">Height</font><font style="font-size:12px">=</font><font style="font-size:12px;color:blue">&quot;150&quot;</font><font style="font-size:12px"> </font><font style="font-size:12px;color:red">Width</font><font style="font-size:12px">=</font><font style="font-size:12px;color:blue">&quot;200&quot;</font><font style="font-size:12px">&gt;  </font></font></font></font></td></tr> <tr> <td style="background-color:#f7f7f7"><font style="font-size:12px"><font style="font-size:12px"><font style="font-size:12px">    </font><font style="font-size:12px;color:blue">&lt;</font><font style="font-size:12px" color="#990000">Grid</font><font style="font-size:12px;color:blue">&gt;</font><font style="font-size:12px"> </font></font></font></font></td></tr> <tr> <td><font style="font-size:12px"><font style="font-size:12px"><font style="font-size:12px">        </font><font style="font-size:12px;color:blue">&lt;</font><font style="font-size:12px"><font style="font-size:12px" color="#990000">Grid</font>.<font style="font-size:12px" color="#990000">RowDefinitions</font></font><font style="font-size:12px;color:blue">&gt;</font><font style="font-size:12px"> </font></font></font></font></td></tr> <tr> <td style="background-color:#f7f7f7"><font style="font-size:12px"><font style="font-size:12px"><font style="font-size:12px">            </font><font style="font-size:12px;color:blue">&lt;</font><font style="font-size:12px"><font style="font-size:12px" color="#990000">RowDefinition</font> </font><font style="font-size:12px;color:red">Height</font><font style="font-size:12px">=</font><font style="font-size:12px;color:blue">&quot;*&quot;</font><font style="font-size:12px">/&gt;  </font></font></font></font></td></tr> <tr> <td><font style="font-size:12px"><font style="font-size:12px"><font style="font-size:12px">            </font><font style="font-size:12px;color:blue">&lt;</font><font style="font-size:12px"><font style="font-size:12px" color="#990000">RowDefinition</font> </font><font style="font-size:12px;color:red">Height</font><font style="font-size:12px">=</font><font style="font-size:12px;color:blue">&quot;Auto&quot;</font><font style="font-size:12px">/&gt;  </font></font></font></font></td></tr> <tr> <td style="background-color:#f7f7f7"><font style="font-size:12px"><font style="font-size:12px"><font style="font-size:12px">        </font><font style="font-size:12px;color:blue">&lt;/</font><font style="font-size:12px"><font style="font-size:12px" color="#990000">Grid</font>.<font style="font-size:12px" color="#990000">RowDefinitions</font></font><font style="font-size:12px;color:blue">&gt;</font><font style="font-size:12px"> </font></font></font></font></td></tr> <tr> <td><font style="font-size:12px"><font style="font-size:12px"><font style="font-size:12px">        </font><font style="font-size:12px;color:blue">&lt;</font><font style="font-size:12px"><font style="font-size:12px" color="#990000">ListBox</font> </font><font style="font-size:12px;color:red">ItemsSource</font><font style="font-size:12px">=</font><font style="font-size:12px;color:blue">&quot;{Binding}&quot;</font><font style="font-size:12px"> </font></font></font></font></td></tr> <tr> <td style="background-color:#f7f7f7"><font style="font-size:12px"><font style="font-size:12px"><font style="font-size:12px">                 <font style="font-size:12px;background-color:#cccccc"><font style="font-size:12px" color="#ff0000">w</font>:<font style="font-size:12px;color:red">ListBox.AutoScroll</font><font style="font-size:12px">=</font><font style="font-size:12px;color:blue">&quot;True&quot;</font></font></font><font style="font-size:12px"> </font><font style="font-size:12px;color:blue">/&gt;</font><font style="font-size:12px"> </font></font></font></font></td></tr> <tr> <td><font style="font-size:12px"><font style="font-size:12px"><font style="font-size:12px">        </font><font style="font-size:12px;color:blue">&lt;</font><font style="font-size:12px"><font style="font-size:12px" color="#990000">Button</font> </font><font style="font-size:12px;color:red">Grid.Row</font><font style="font-size:12px">=</font><font style="font-size:12px;color:blue">&quot;1&quot;</font><font style="font-size:12px"> </font></font></font></font></td></tr> <tr> <td style="background-color:#f7f7f7"><font style="font-size:12px"><font style="font-size:12px"><font style="font-size:12px">                </font><font style="font-size:12px;color:red">Click</font><font style="font-size:12px">=</font><font style="font-size:12px;color:blue">&quot;Add_Click&quot;</font><font style="font-size:12px">&gt;Add item</font><font style="font-size:12px;color:blue">&lt;/</font><font style="font-size:12px" color="#990000">Button</font><font style="font-size:12px;color:blue">&gt;</font><font style="font-size:12px"> </font></font></font></font></td></tr> <tr> <td><font style="font-size:12px"><font style="font-size:12px"><font style="font-size:12px">    </font><font style="font-size:12px;color:blue">&lt;/</font><font style="font-size:12px" color="#990000">Grid</font><font style="font-size:12px;color:blue">&gt;</font><font style="font-size:12px"> </font></font></font></font></td></tr> <tr> <td style="background-color:#f7f7f7"></font><font style="font-size:12px"><font style="font-size:12px"><font style="font-size:12px"><font style="font-size:12px;color:blue">&lt;/</font><font style="font-size:12px" color="#990000">Window</font><font style="font-size:12px;color:blue">&gt;</font><font style="font-size:12px"> </font></font></font></font></td></tr> <tr> <td> </td></tr></tbody></table></div></font><font color="#0000ff" size=2> <p></font><br>Corresponding C# code-behind is trivial. It provides example implementation and have no mention about auto-scrolling:</p> <div style="border-right:#7f9db9 1px solid;border-top:#7f9db9 1px solid;font-size:11px;overflow:auto;border-left:#7f9db9 1px solid;line-height:100%! important;border-bottom:#7f9db9 1px solid;font-family:Courier New;background-color:white"> <table style="border-top-width:0px;border-left-width:0px;margin:2px 0px;width:99%;border-bottom:#eee 0px solid;border-collapse:collapse;background-color:#fff;border-right-width:0px" cellspacing=0 cellpadding=0> <colgroup> <col style="padding-left:10px;font-size:11px;border-bottom:#f7f7f7 1px solid;font-family:Courier New;white-space:nowrap"> <tbody> <tr> <td><font style="font-size:11px"></font><font style="color:blue"><br><font style="font-size:12px">using</font></font><font style="font-size:12px"> System.Collections.ObjectModel;  </font></td></tr> <tr> <td style="background-color:#f7f7f7"></font><font style="font-size:12px"><font style="font-size:12px"><font style="font-size:12px"><font style="font-size:12px;color:blue">using</font><font style="font-size:12px"> System.Windows;  </font></font></font></font></td></tr> <tr> <td><font style="font-size:12px"> </font></td></tr> <tr> <td style="background-color:#f7f7f7"></font><font style="font-size:12px"><font style="font-size:12px"><font style="font-size:12px"><font style="font-size:12px;color:blue">namespace</font><font style="font-size:12px"> ListBoxAutoScrollTest  </font></font></font></font></td></tr> <tr> <td><font style="font-size:12px">{  </font></td></tr> <tr> <td style="background-color:#f7f7f7"><font style="font-size:12px"><font style="font-size:12px"><font style="font-size:12px">    </font><font style="font-size:12px;color:blue">public</font><font style="font-size:12px"> <font style="font-size:12px" color="#000099">partial</font> </font><font style="font-size:12px;color:blue">class</font><font style="font-size:12px"> <font style="font-size:12px" color="#339999">Window1</font> : <font style="font-size:12px" color="#339999">Window</font>  </font></font></font></font></td></tr> <tr> <td><font style="font-size:12px">    {  </font></td></tr> <tr> <td style="background-color:#f7f7f7"><font style="font-size:12px">        <font style="font-size:12px" color="#339999">ObservableCollection</font>&lt;<font style="font-size:12px" color="#339999">Person</font>&gt; People;  </font></td></tr> <tr> <td><font style="font-size:12px"><font style="font-size:12px"><font style="font-size:12px">        </font><font style="font-size:12px;color:blue">public</font><font style="font-size:12px"> Window1()  </font></font></font></font></td></tr> <tr> <td style="background-color:#f7f7f7"><font style="font-size:12px">        {  </font></td></tr> <tr> <td><font style="font-size:12px"> </font></td></tr> <tr> <td style="background-color:#f7f7f7"><font style="font-size:12px">            InitializeComponent();  </font></td></tr> <tr> <td><font style="font-size:12px"> </font></td></tr> <tr> <td style="background-color:#f7f7f7"><font style="font-size:12px"><font style="font-size:12px"><font style="font-size:12px">            People = </font><font style="font-size:12px;color:blue">new</font><font style="font-size:12px"> <font style="font-size:12px" color="#339999">ObservableCollection</font>&lt;<font style="font-size:12px" color="#339999">Person</font>&gt;();  </font></font></font></font></td></tr> <tr> <td><font style="font-size:12px"><font style="font-size:12px"><font style="font-size:12px">            People.Add(</font><font style="font-size:12px;color:blue">new</font><font style="font-size:12px"> <font style="font-size:12px" color="#339999">Person</font>(</font><font style="font-size:12px;color:blue" color="#660000">&quot;Jack&quot;</font><font style="font-size:12px">));  </font></font></font></font></td></tr> <tr> <td style="background-color:#f7f7f7"><font style="font-size:12px"><font style="font-size:12px"><font style="font-size:12px">            People.Add(</font><font style="font-size:12px;color:blue">new</font><font style="font-size:12px"> <font style="font-size:12px" color="#339999">Person</font>(</font><font style="font-size:12px;color:blue">&quot;<font style="font-size:12px">Michael</font>&quot;</font><font style="font-size:12px">));  </font></font></font></font></td></tr> <tr> <td><font style="font-size:12px"><font style="font-size:12px"><font style="font-size:12px">            People.Add(</font><font style="font-size:12px;color:blue">new</font><font style="font-size:12px"> <font style="font-size:12px" color="#339999">Person</font>(</font><font style="font-size:12px;color:blue">&quot;Leonard&quot;</font><font style="font-size:12px">));  </font></font></font></font></td></tr> <tr> <td style="background-color:#f7f7f7"><font style="font-size:12px"><font style="font-size:12px"><font style="font-size:12px">            People.Add(</font><font style="font-size:12px;color:blue">new</font><font style="font-size:12px"> <font style="font-size:12px" color="#339999">Person</font>(</font><font style="font-size:12px;color:blue">&quot;Arthur&quot;</font><font style="font-size:12px">));  </font></font></font></font></td></tr> <tr> <td><font style="font-size:12px"><font style="font-size:12px"><font style="font-size:12px">            People.Add(</font><font style="font-size:12px;color:blue">new</font><font style="font-size:12px"> <font style="font-size:12px" color="#339999">Person</font>(</font><font style="font-size:12px;color:blue">&quot;Robert&quot;</font><font style="font-size:12px">));  </font></font></font></font></td></tr> <tr> <td style="background-color:#f7f7f7"><font style="font-size:12px"> </font></td></tr> <tr> <td><font style="font-size:12px">            DataContext = People;  </font></td></tr> <tr> <td style="background-color:#f7f7f7"><font style="font-size:12px"> </font></td></tr> <tr> <td><font style="font-size:12px">        }  </font></td></tr> <tr> <td style="background-color:#f7f7f7"><font style="font-size:12px"> </font></td></tr> <tr> <td><font style="font-size:12px"><font style="font-size:12px"><font style="font-size:12px">        </font><font style="font-size:12px;color:blue">private</font><font style="font-size:12px"> </font><font style="font-size:12px;color:blue">void</font><font style="font-size:12px"> Add_Click(</font><font style="font-size:12px;color:blue">object</font><font style="font-size:12px"> sender, <font style="font-size:12px" color="#339999">RoutedEventArgs</font> e)  </font></font></font></font></td></tr> <tr> <td style="background-color:#f7f7f7"><font style="font-size:12px">        {  </font></td></tr> <tr> <td><font style="font-size:12px"><font style="font-size:12px"><font style="font-size:12px">            People.Add(</font><font style="font-size:12px;color:blue">new</font><font style="font-size:12px"> <font style="font-size:12px" color="#339999">Person</font>(</font><font style="font-size:12px;color:blue">&quot;Person # &quot;</font><font style="font-size:12px"> + (People.Count + 1)));  </font></font></font></font></td></tr> <tr> <td style="background-color:#f7f7f7"><font style="font-size:12px">        }  </font></td></tr> <tr> <td><font style="font-size:12px"> </font></td></tr> <tr> <td style="background-color:#f7f7f7"><font style="font-size:12px">    }  </font></td></tr> <tr> <td><font style="font-size:12px"> </font></td></tr> <tr> <td style="background-color:#f7f7f7"><font style="font-size:12px"><font style="font-size:12px"><font style="font-size:12px">    </font><font style="font-size:12px;color:blue">public</font><font style="font-size:12px"> </font><font style="font-size:12px;color:blue">class</font><font style="font-size:12px"> <font style="font-size:12px" color="#339999">Person</font>  </font></font></font></font></td></tr> <tr> <td><font style="font-size:12px">    {  </font></td></tr> <tr> <td style="background-color:#f7f7f7"><font style="font-size:12px"><font style="font-size:12px"><font style="font-size:12px">        </font><font style="font-size:12px;color:blue">public</font><font style="font-size:12px"> </font><font style="font-size:12px;color:blue">string</font><font style="font-size:12px"> Name { </font><font style="font-size:12px;color:blue">get</font><font style="font-size:12px">; </font><font style="font-size:12px;color:blue">private</font><font style="font-size:12px"> </font><font style="font-size:12px;color:blue">set</font><font style="font-size:12px">; }  </font></font></font></font></td></tr> <tr> <td><font style="font-size:12px"><font style="font-size:12px"><font style="font-size:12px">        </font><font style="font-size:12px;color:blue">public</font><font style="font-size:12px"> Person(</font><font style="font-size:12px;color:blue">string</font><font style="font-size:12px"> name)  </font></font></font></font></td></tr> <tr> <td style="background-color:#f7f7f7"><font style="font-size:12px">        {  </font></td></tr> <tr> <td><font style="font-size:12px">            Name = name;  </font></td></tr> <tr> <td style="background-color:#f7f7f7"><font style="font-size:12px">        }  </font></td></tr> <tr> <td><font style="font-size:12px"><font style="font-size:12px"><font style="font-size:12px">        </font><font style="font-size:12px;color:blue">public</font><font style="font-size:12px"> </font><font style="font-size:12px;color:blue">override</font><font style="font-size:12px"> </font><font style="font-size:12px;color:blue">string</font><font style="font-size:12px"> ToString()  </font></font></font></font></td></tr> <tr> <td style="background-color:#f7f7f7"><font style="font-size:12px">        {  </font></td></tr> <tr> <td><font style="font-size:12px"><font style="font-size:12px"><font style="font-size:12px">            </font><font style="font-size:12px;color:blue">return</font><font style="font-size:12px"> Name;  </font></font></font></font></td></tr> <tr> <td style="background-color:#f7f7f7"><font style="font-size:12px">        }  </font></td></tr> <tr> <td><font style="font-size:12px">    }  </font></td></tr> <tr> <td style="background-color:#f7f7f7"><font style="font-size:12px"> </font></td></tr> <tr> <td><font style="font-size:12px">}</font></td></tr> <tr> <td style="background-color:#f7f7f7"><font style="font-size:12px"></font></td></tr> <tr> <td> </td></tr></tbody></table></div> <p><br>All the auto-scroll implementation stuff we put into separate application code file.<br><br>Here we listen to <font color="#3333ff">ListBox.ItemsSource</font> property changes, subscribe to <font color="#3333ff">NotifyCollectionChanged</font> event of underlying collection and call <font color="#3333ff">ListBox.ScrollIntoView()</font> method when new items are added: </p> <div style="border-right:#7f9db9 1px solid;border-top:#7f9db9 1px solid;font-size:11px;overflow:auto;border-left:#7f9db9 1px solid;line-height:100%! important;border-bottom:#7f9db9 1px solid;font-family:Courier New;background-color:white"> <table style="border-top-width:0px;border-left-width:0px;margin:2px 0px;width:99%;border-bottom:#eee 0px solid;border-collapse:collapse;background-color:#fff;border-right-width:0px" cellspacing=0 cellpadding=0> <colgroup> <col style="padding-left:10px;font-size:11px;border-bottom:#f7f7f7 1px solid;font-family:Courier New;white-space:nowrap"> <tbody> <tr> <td><font style="font-size:11px"></font><font style="color:blue"><br><font style="font-size:12px">using</font></font><font style="font-size:12px"> System.Collections;  </font></td></tr> <tr> <td style="background-color:#f7f7f7"></font><font style="font-size:12px"><font style="font-size:14px"><font style="font-size:12px"><font style="font-size:12px;color:blue">using</font><font style="font-size:12px"> System.Collections.Generic;  </font></font></font></font></td></tr> <tr> <td></font><font style="font-size:12px"><font style="font-size:14px"><font style="font-size:12px"><font style="font-size:12px;color:blue">using</font><font style="font-size:12px"> System.Collections.ObjectModel;  </font></font></font></font></td></tr> <tr> <td style="background-color:#f7f7f7"></font><font style="font-size:12px"><font style="font-size:14px"><font style="font-size:12px"><font style="font-size:12px;color:blue">using</font><font style="font-size:12px"> System.Collections.Specialized;  </font></font></font></font></td></tr> <tr> <td></font><font style="font-size:12px"><font style="font-size:14px"><font style="font-size:12px"><font style="font-size:12px;color:blue">using</font><font style="font-size:12px"> System.Windows.Data;  </font></font></font></font></td></tr> <tr> <td style="background-color:#f7f7f7"><font style="font-size:12px"> </font></td></tr> <tr> <td></font><font style="font-size:12px"><font style="font-size:14px"><font style="font-size:12px"><font style="font-size:12px;color:blue">namespace</font><font style="font-size:12px"> System.Windows.Workarounds  </font></font></font></font></td></tr> <tr> <td style="background-color:#f7f7f7"><font style="font-size:12px">{  </font></td></tr> <tr> <td><font style="font-size:12px"> </font></td></tr> <tr> <td style="background-color:#f7f7f7"><font style="font-size:14px"><font style="font-size:12px"><font style="font-size:12px">    </font><font style="font-size:12px;color:blue">public</font><font style="font-size:12px"> </font><font style="font-size:12px;color:blue">static</font><font style="font-size:12px"> </font><font style="font-size:12px;color:blue">class</font><font style="font-size:12px"> <font style="font-size:12px" color="#339999">ListBox</font>  </font></font></font></font></td></tr> <tr> <td><font style="font-size:12px">    {  </font></td></tr> <tr> <td style="background-color:#f7f7f7"><font style="font-size:12px"> </font></td></tr> <tr> <td><font style="font-size:14px"><font style="font-size:12px"><font style="font-size:12px">        </font><font style="font-size:12px;color:blue">public</font><font style="font-size:12px"> </font><font style="font-size:12px;color:blue">static</font><font style="font-size:12px"> </font><font style="font-size:12px;color:blue">readonly</font><font style="font-size:12px"> <font style="font-size:12px" color="#339999">DependencyProperty</font> AutoScrollProperty =  </font></font></font></font></td></tr> <tr> <td style="background-color:#f7f7f7"><font style="font-size:14px"><font style="font-size:12px"><font style="font-size:12px">            <font style="font-size:12px" color="#339999">DependencyProperty</font>.RegisterAttached(</font><font style="font-size:12px;color:blue">&quot;AutoScroll&quot;</font><font style="font-size:12px">, </font><font style="font-size:12px;color:blue">typeof</font><font style="font-size:12px">(</font><font style="font-size:12px;color:blue">bool</font><font style="font-size:12px">), </font><font style="font-size:12px;color:blue">typeof</font><font style="font-size:12px">(System.Windows.Controls.<font style="font-size:12px" color="#339999">ListBox</font>),  </font></font></font></font></td></tr> <tr> <td><font style="font-size:14px"><font style="font-size:12px"><font style="font-size:12px">            </font><font style="font-size:12px;color:blue">new</font><font style="font-size:12px"> <font style="font-size:12px" color="#339999">PropertyMetadata</font>(</font><font style="font-size:12px;color:blue">false</font><font style="font-size:12px">));  </font></font></font></font></td></tr> <tr> <td style="background-color:#f7f7f7"><font style="font-size:12px"> </font></td></tr> <tr> <td><font style="font-size:14px"><font style="font-size:12px"><font style="font-size:12px">        </font><font style="font-size:12px;color:blue">public</font><font style="font-size:12px"> </font><font style="font-size:12px;color:blue">static</font><font style="font-size:12px"> </font><font style="font-size:12px;color:blue">readonly</font><font style="font-size:12px"> <font style="font-size:12px" color="#339999">DependencyProperty</font> AutoScrollHandlerProperty =  </font></font></font></font></td></tr> <tr> <td style="background-color:#f7f7f7"><font style="font-size:14px"><font style="font-size:12px"><font style="font-size:12px">            <font style="font-size:12px" color="#339999">DependencyProperty</font>.RegisterAttached(</font><font style="font-size:12px;color:blue">&quot;AutoScrollHandler&quot;</font><font style="font-size:12px">, </font><font style="font-size:12px;color:blue">typeof</font><font style="font-size:12px">(<font style="font-size:12px" color="#339999">AutoScrollHandler</font>), </font><font style="font-size:12px;color:blue">typeof</font><font style="font-size:12px">(System.Windows.Controls.<font style="font-size:12px" color="#339999">ListBox</font>));  </font></font></font></font></td></tr> <tr> <td><font style="font-size:12px"> </font></td></tr> <tr> <td style="background-color:#f7f7f7"><font style="font-size:14px"><font style="font-size:12px"><font style="font-size:12px">        </font><font style="font-size:12px;color:blue">public</font><font style="font-size:12px"> </font><font style="font-size:12px;color:blue">static</font><font style="font-size:12px"> </font><font style="font-size:12px;color:blue">bool</font><font style="font-size:12px"> GetAutoScroll(System.Windows.Controls.<font style="font-size:12px" color="#339999">ListBox</font> instance)  </font></font></font></font></td></tr> <tr> <td><font style="font-size:12px">        {  </font></td></tr> <tr> <td style="background-color:#f7f7f7"><font style="font-size:14px"><font style="font-size:12px"><font style="font-size:12px">            </font><font style="font-size:12px;color:blue">return</font><font style="font-size:12px"> (</font><font style="font-size:12px;color:blue">bool</font><font style="font-size:12px">)instance.GetValue(AutoScrollProperty);  </font></font></font></font></td></tr> <tr> <td><font style="font-size:12px">        }  </font></td></tr> <tr> <td style="background-color:#f7f7f7"><font style="font-size:12px"> </font></td></tr> <tr> <td><font style="font-size:14px"><font style="font-size:12px"><font style="font-size:12px">        </font><font style="font-size:12px;color:blue">public</font><font style="font-size:12px"> </font><font style="font-size:12px;color:blue">static</font><font style="font-size:12px"> </font><font style="font-size:12px;color:blue">void</font><font style="font-size:12px"> SetAutoScroll(System.Windows.Controls.<font style="font-size:12px" color="#339999">ListBox</font> instance, </font><font style="font-size:12px;color:blue">bool</font><font style="font-size:12px"> value)  </font></font></font></font></td></tr> <tr> <td style="background-color:#f7f7f7"><font style="font-size:12px">        {  </font></td></tr> <tr> <td><font style="font-size:12px">            <font style="font-size:12px" color="#339999">AutoScrollHandler</font> OldHandler = (<font style="font-size:12px" color="#339999">AutoScrollHandler</font>)instance.GetValue(AutoScrollHandlerProperty);  </font></td></tr> <tr> <td style="background-color:#f7f7f7"><font style="font-size:14px"><font style="font-size:12px"><font style="font-size:12px">            </font><font style="font-size:12px;color:blue">if</font><font style="font-size:12px"> (OldHandler != </font><font style="font-size:12px;color:blue">null</font><font style="font-size:12px">)  </font></font></font></font></td></tr> <tr> <td><font style="font-size:12px">            {  </font></td></tr> <tr> <td style="background-color:#f7f7f7"><font style="font-size:12px">                OldHandler.Dispose();  </font></td></tr> <tr> <td><font style="font-size:14px"><font style="font-size:12px"><font style="font-size:12px">                instance.SetValue(AutoScrollHandlerProperty, </font><font style="font-size:12px;color:blue">null</font><font style="font-size:12px">);  </font></font></font></font></td></tr> <tr> <td style="background-color:#f7f7f7"><font style="font-size:12px">            }  </font></td></tr> <tr> <td><font style="font-size:12px">            instance.SetValue(AutoScrollProperty, value);  </font></td></tr> <tr> <td style="background-color:#f7f7f7"><font style="font-size:14px"><font style="font-size:12px"><font style="font-size:12px">            </font><font style="font-size:12px;color:blue">if</font><font style="font-size:12px"> (value)  </font></font></font></font></td></tr> <tr> <td><font style="font-size:14px"><font style="font-size:12px"><font style="font-size:12px">                instance.SetValue(AutoScrollHandlerProperty, </font><font style="font-size:12px;color:blue">new</font><font style="font-size:12px"> <font style="font-size:12px" color="#339999">AutoScrollHandler</font>(instance));  </font></font></font></font></td></tr> <tr> <td style="background-color:#f7f7f7"><font style="font-size:12px">        }  </font></td></tr> <tr> <td><font style="font-size:12px"> </font></td></tr> <tr> <td style="background-color:#f7f7f7"><font style="font-size:12px">    }  </font></td></tr> <tr> <td><font style="font-size:12px"> </font></td></tr> <tr> <td style="background-color:#f7f7f7"><font style="font-size:14px"><font style="font-size:12px"><font style="font-size:12px">    </font><font style="font-size:12px;color:blue">public</font><font style="font-size:12px"> </font><font style="font-size:12px;color:blue">class</font><font style="font-size:12px"> <font style="font-size:12px" color="#339999">AutoScrollHandler</font> : <font style="font-size:12px" color="#339999">DependencyObject</font>, <font style="font-size:12px" color="#339999">IDisposable</font>  </font></font></font></font></td></tr> <tr> <td><font style="font-size:12px">    {  </font></td></tr> <tr> <td style="background-color:#f7f7f7"><font style="font-size:12px"> </font></td></tr> <tr> <td><font style="font-size:14px"><font style="font-size:12px"><font style="font-size:12px">        </font><font style="font-size:12px;color:blue">public</font><font style="font-size:12px"> </font><font style="font-size:12px;color:blue">static</font><font style="font-size:12px"> </font><font style="font-size:12px;color:blue">readonly</font><font style="font-size:12px"> <font style="font-size:12px" color="#339999">DependencyProperty</font> ItemsSourceProperty =  </font></font></font></font></td></tr> <tr> <td style="background-color:#f7f7f7"><font style="font-size:14px"><font style="font-size:12px"><font style="font-size:12px">            <font style="font-size:12px" color="#339999">DependencyProperty</font>.Register(</font><font style="font-size:12px;color:blue">&quot;ItemsSource&quot;</font><font style="font-size:12px">, </font><font style="font-size:12px;color:blue">typeof</font><font style="font-size:12px">(<font style="font-size:12px" color="#339999">IEnumerable</font>),  </font></font></font></font></td></tr> <tr> <td><font style="font-size:14px"><font style="font-size:12px"><font style="font-size:12px">            </font><font style="font-size:12px;color:blue">typeof</font><font style="font-size:12px">(<font style="font-size:12px" color="#339999">AutoScrollHandler</font>), </font><font style="font-size:12px;color:blue">new</font><font style="font-size:12px"> <font style="font-size:12px" color="#339999">FrameworkPropertyMetadata</font>(</font><font style="font-size:12px;color:blue">null</font><font style="font-size:12px">, <font style="font-size:12px" color="#339999">FrameworkPropertyMetadataOptions</font>.None,  </font></font></font></font></td></tr> <tr> <td style="background-color:#f7f7f7"><font style="font-size:14px"><font style="font-size:12px"><font style="font-size:12px">                </font><font style="font-size:12px;color:blue">new</font><font style="font-size:12px"> <font style="font-size:12px" color="#339999">PropertyChangedCallback</font>(ItemsSourcePropertyChanged)));  </font></font></font></font></td></tr> <tr> <td><font style="font-size:12px"> </font></td></tr> <tr> <td style="background-color:#f7f7f7"><font style="font-size:14px"><font style="font-size:12px"><font style="font-size:12px">        </font><font style="font-size:12px;color:blue">private</font><font style="font-size:12px"> System.Windows.Controls.<font style="font-size:12px" color="#339999">ListBox</font> Target;  </font></font></font></font></td></tr> <tr> <td><font style="font-size:12px"> </font></td></tr> <tr> <td style="background-color:#f7f7f7"><font style="font-size:14px"><font style="font-size:12px"><font style="font-size:12px">        </font><font style="font-size:12px;color:blue">public</font><font style="font-size:12px"> AutoScrollHandler(System.Windows.Controls.<font style="font-size:12px" color="#339999">ListBox</font> target)  </font></font></font></font></td></tr> <tr> <td><font style="font-size:12px">        {  </font></td></tr> <tr> <td style="background-color:#f7f7f7"><font style="font-size:12px">            Target = target;  </font></td></tr> <tr> <td><font style="font-size:14px"><font style="font-size:12px"><font style="font-size:12px">            <font style="font-size:12px" color="#339999">Binding</font> B = </font><font style="font-size:12px;color:blue">new</font><font style="font-size:12px"> <font style="font-size:12px" color="#339999">Binding</font>(</font><font style="font-size:12px;color:blue">&quot;ItemsSource&quot;</font><font style="font-size:12px">);  </font></font></font></font></td></tr> <tr> <td style="background-color:#f7f7f7"><font style="font-size:12px">            B.Source = Target;  </font></td></tr> <tr> <td><font style="font-size:14px"><font style="font-size:12px"><font style="font-size:12px">            <font style="font-size:12px" color="#339999">BindingOperations</font>.SetBinding(</font><font style="font-size:12px;color:blue">this</font><font style="font-size:12px">, ItemsSourceProperty, B);  </font></font></font></font></td></tr> <tr> <td style="background-color:#f7f7f7"><font style="font-size:12px">        }  </font></td></tr> <tr> <td><font style="font-size:12px"> </font></td></tr> <tr> <td style="background-color:#f7f7f7"><font style="font-size:14px"><font style="font-size:12px"><font style="font-size:12px">        </font><font style="font-size:12px;color:blue">public</font><font style="font-size:12px"> </font><font style="font-size:12px;color:blue">void</font><font style="font-size:12px"> Dispose()  </font></font></font></font></td></tr> <tr> <td><font style="font-size:12px">        {  </font></td></tr> <tr> <td style="background-color:#f7f7f7"><font style="font-size:14px"><font style="font-size:12px"><font style="font-size:12px">            <font style="font-size:12px" color="#339999">BindingOperations</font>.ClearBinding(</font><font style="font-size:12px;color:blue">this</font><font style="font-size:12px">, ItemsSourceProperty);  </font></font></font></font></td></tr> <tr> <td><font style="font-size:12px">        }  </font></td></tr> <tr> <td style="background-color:#f7f7f7"><font style="font-size:12px"> </font></td></tr> <tr> <td><font style="font-size:14px"><font style="font-size:12px"><font style="font-size:12px">        </font><font style="font-size:12px;color:blue">public</font><font style="font-size:12px"> <font style="font-size:12px" color="#339999">IEnumerable</font> ItemsSource  </font></font></font></font></td></tr> <tr> <td style="background-color:#f7f7f7"><font style="font-size:12px">        {  </font></td></tr> <tr> <td><font style="font-size:14px"><font style="font-size:12px"><font style="font-size:12px">            </font><font style="font-size:12px;color:blue">get</font><font style="font-size:12px"> { </font><font style="font-size:12px;color:blue">return</font><font style="font-size:12px"> (<font style="font-size:12px" color="#339999">IEnumerable</font>)GetValue(ItemsSourceProperty); }  </font></font></font></font></td></tr> <tr> <td style="background-color:#f7f7f7"><font style="font-size:14px"><font style="font-size:12px"><font style="font-size:12px">            </font><font style="font-size:12px;color:blue">set</font><font style="font-size:12px"> { SetValue(ItemsSourceProperty, <font style="font-size:12px" color="#000099">value</font>); }  </font></font></font></font></td></tr> <tr> <td><font style="font-size:12px">        }  </font></td></tr> <tr> <td style="background-color:#f7f7f7"><font style="font-size:12px"> </font></td></tr> <tr> <td><font style="font-size:14px"><font style="font-size:12px"><font style="font-size:12px">        </font><font style="font-size:12px;color:blue">static</font><font style="font-size:12px"> </font><font style="font-size:12px;color:blue">void</font><font style="font-size:12px"> ItemsSourcePropertyChanged(<font style="font-size:12px" color="#339999">DependencyObject</font> o, <font style="font-size:12px" color="#339999">DependencyPropertyChangedEventArgs</font> e)  </font></font></font></font></td></tr> <tr> <td style="background-color:#f7f7f7"><font style="font-size:12px">        {  </font></td></tr> <tr> <td><font style="font-size:12px">            ((<font style="font-size:12px" color="#339999">AutoScrollHandler</font>)o).ItemsSourceChanged((<font style="font-size:12px" color="#339999">IEnumerable</font>)e.OldValue, (<font style="font-size:12px" color="#339999">IEnumerable</font>)e.NewValue);  </font></td></tr> <tr> <td style="background-color:#f7f7f7"><font style="font-size:12px">        }  </font></td></tr> <tr> <td><font style="font-size:12px"> </font></td></tr> <tr> <td style="background-color:#f7f7f7"><font style="font-size:14px"><font style="font-size:12px"><font style="font-size:12px">        </font><font style="font-size:12px;color:blue">void</font><font style="font-size:12px"> ItemsSourceChanged(<font style="font-size:12px" color="#339999">IEnumerable</font> oldValue, <font style="font-size:12px" color="#339999">IEnumerable</font> newValue)  </font></font></font></font></td></tr> <tr> <td><font style="font-size:12px">        {  </font></td></tr> <tr> <td style="background-color:#f7f7f7"><font style="font-size:14px"><font style="font-size:12px"><font style="font-size:12px">            <font style="font-size:12px" color="#339999">INotifyCollectionChanged</font> Collection = oldValue </font><font style="font-size:12px;color:blue">as</font><font style="font-size:12px"> <font style="font-size:12px" color="#339999">INotifyCollectionChanged</font>;  </font></font></font></font></td></tr> <tr> <td><font style="font-size:14px"><font style="font-size:12px"><font style="font-size:12px">            </font><font style="font-size:12px;color:blue">if</font><font style="font-size:12px"> (Collection != </font><font style="font-size:12px;color:blue">null</font><font style="font-size:12px">)  </font></font></font></font></td></tr> <tr> <td style="background-color:#f7f7f7"><font style="font-size:14px"><font style="font-size:12px"><font style="font-size:12px">                Collection.CollectionChanged -= </font><font style="font-size:12px;color:blue">new</font><font style="font-size:12px"> <font style="font-size:12px" color="#339999">NotifyCollectionChangedEventHandler</font>(Collection_CollectionChanged);  </font></font></font></font></td></tr> <tr> <td><font style="font-size:14px"><font style="font-size:12px"><font style="font-size:12px">            Collection = newValue </font><font style="font-size:12px;color:blue">as</font><font style="font-size:12px"> <font style="font-size:12px" color="#339999">INotifyCollectionChanged</font>;  </font></font></font></font></td></tr> <tr> <td style="background-color:#f7f7f7"><font style="font-size:14px"><font style="font-size:12px"><font style="font-size:12px">            </font><font style="font-size:12px;color:blue">if</font><font style="font-size:12px"> (Collection != </font><font style="font-size:12px;color:blue">null</font><font style="font-size:12px">)  </font></font></font></font></td></tr> <tr> <td><font style="font-size:14px"><font style="font-size:12px"><font style="font-size:12px">                Collection.CollectionChanged += </font><font style="font-size:12px;color:blue">new</font><font style="font-size:12px"> <font style="font-size:12px" color="#339999">NotifyCollectionChangedEventHandler</font>(Collection_CollectionChanged);  </font></font></font></font></td></tr> <tr> <td style="background-color:#f7f7f7"><font style="font-size:12px">        }  </font></td></tr> <tr> <td><font style="font-size:12px"> </font></td></tr> <tr> <td style="background-color:#f7f7f7"><font style="font-size:14px"><font style="font-size:12px"><font style="font-size:12px">        </font><font style="font-size:12px;color:blue">void</font><font style="font-size:12px"> Collection_CollectionChanged(</font><font style="font-size:12px;color:blue">object</font><font style="font-size:12px"> sender, <font style="font-size:12px" color="#339999">NotifyCollectionChangedEventArgs</font> e)  </font></font></font></font></td></tr> <tr> <td><font style="font-size:12px">        {  </font></td></tr> <tr> <td style="background-color:#f7f7f7"><font style="font-size:14px"><font style="font-size:12px"><font style="font-size:12px">            </font><font style="font-size:12px;color:blue">if</font><font style="font-size:12px"> (e.Action != <font style="font-size:12px" color="#339999">NotifyCollectionChangedAction</font>.Add || e.NewItems == </font><font style="font-size:12px;color:blue">null</font><font style="font-size:12px"> || e.NewItems.Count &lt; 1)  </font></font></font></font></td></tr> <tr> <td><font style="font-size:14px"><font style="font-size:12px"><font style="font-size:12px">                </font><font style="font-size:12px;color:blue">return</font><font style="font-size:12px">;  </font></font></font></font></td></tr> <tr> <td style="background-color:#f7f7f7"><font style="font-size:12px">            Target.ScrollIntoView(e.NewItems[e.NewItems.Count - 1]);  </font></td></tr> <tr> <td><font style="font-size:12px">        }  </font></td></tr> <tr> <td style="background-color:#f7f7f7"><font style="font-size:12px"> </font></td></tr> <tr> <td><font style="font-size:12px">    }  </font></td></tr> <tr> <td style="background-color:#f7f7f7"><font style="font-size:12px"> </font></td></tr> <tr> <td><font style="font-size:12px">} <br></font></td></tr></tbody></table></div> <p><br>Hope, this helps.</p>Sun, 09 Nov 2008 22:07:42 Z2008-11-09T22:11:51Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/0f524459-b14e-4f9a-8264-267953418a2d#979c715d-c081-4e32-98b2-4de272d1442fhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/0f524459-b14e-4f9a-8264-267953418a2d#979c715d-c081-4e32-98b2-4de272d1442fBorka_http://social.msdn.microsoft.com/Profile/en-US/?user=Borka_Trivial ListBox/ListView Autoscroll Well, this could work, just like other similar solutions I mentioned. The only problem is that I was looking for some really trivial solution, while the idea above demands writing 2 extra classes(!) with ~60(!) lines of code for something I would expect to look like: &quot;&lt;ListBox AutoScroll=&quot;true&quot; /&gt;&quot;<br> <br>Thanks anyway.<br>Sun, 09 Nov 2008 23:51:29 Z2008-11-09T23:51:29Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/0f524459-b14e-4f9a-8264-267953418a2d#41941d9f-f8c5-44a9-aaae-571254779109http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/0f524459-b14e-4f9a-8264-267953418a2d#41941d9f-f8c5-44a9-aaae-571254779109Tamir Khasonhttp://social.msdn.microsoft.com/Profile/en-US/?user=Tamir%20KhasonTrivial ListBox/ListView AutoscrollI can propose similar, but smaller solution<br><br>Use this inside OnAutoScrollChanged handler<br><br><div style="overflow:auto;background-color:white;line-height:100% ! important;font-family:Courier New;font-size:11px"><table style="border-width:0px;margin:2px 0px;width:99%;border-collapse:collapse;background-color:rgb(255, 255, 255)" cellpadding=0 cellspacing=0><col style="font-family:Courier New;font-size:11px;padding-left:10px;white-space:nowrap"><tbody><tr><td><font style="font-size:11px">var val = (</font><font style="color:blue">bool</font><font style="font-size:11px">)e.NewValue; </font></td></tr><tr><td style="background-color:rgb(247, 247, 247)">            var lb = s <font style="color:blue">as</font><font style="font-size:11px"> ListBox; </font></td></tr><tr><td>            var ic = lb.Items; </td></tr><tr><td style="background-color:rgb(247, 247, 247)">            var data = ic.SourceCollection <font style="color:blue">as</font><font style="font-size:11px"> INotifyCollectionChanged; </font></td></tr><tr><td> </td></tr><tr><td style="background-color:rgb(247, 247, 247)">            var autoscroller = <font style="color:blue">new</font><font style="font-size:11px"> System.Collections.Specialized.NotifyCollectionChangedEventHandler( </font></td></tr><tr><td>                (s1, e1) =&gt; { </td></tr><tr><td style="background-color:rgb(247, 247, 247)">                    <font style="color:blue">object</font><font style="font-size:11px"> selectedItem = </font><font style="color:blue">default</font><font style="font-size:11px">(</font><font style="color:blue">object</font><font style="font-size:11px">); </font></td></tr><tr><td>                    <font style="color:blue">switch</font><font style="font-size:11px"> (e1.Action) { </font></td></tr><tr><td style="background-color:rgb(247, 247, 247)">                        <font style="color:blue">case</font><font style="font-size:11px"> NotifyCollectionChangedAction.Add: </font></td></tr><tr><td>                        <font style="color:blue">case</font><font style="font-size:11px"> NotifyCollectionChangedAction.Move: selectedItem = e1.NewItems[e1.NewItems.Count - 1]; </font><font style="color:blue">break</font><font style="font-size:11px">; </font></td></tr><tr><td style="background-color:rgb(247, 247, 247)">                        <font style="color:blue">case</font><font style="font-size:11px"> NotifyCollectionChangedAction.Remove: </font><font style="color:blue">if</font><font style="font-size:11px"> (ic.Count &lt; e1.OldStartingIndex) { selectedItem = ic[e1.OldStartingIndex - 1]; } </font><font style="color:blue">else</font><font style="font-size:11px"> </font><font style="color:blue">if</font><font style="font-size:11px"> (ic.Count &gt; 0) selectedItem = ic[0]; </font><font style="color:blue">break</font><font style="font-size:11px">; </font></td></tr><tr><td>                        <font style="color:blue">case</font><font style="font-size:11px"> NotifyCollectionChangedAction.Reset: </font><font style="color:blue">if</font><font style="font-size:11px"> (ic.Count &gt; 0) selectedItem = ic[0]; </font><font style="color:blue">break</font><font style="font-size:11px">; </font></td></tr><tr><td style="background-color:rgb(247, 247, 247)">                    } </td></tr><tr><td> </td></tr><tr><td style="background-color:rgb(247, 247, 247)">                    <font style="color:blue">if</font><font style="font-size:11px"> (selectedItem != </font><font style="color:blue">default</font><font style="font-size:11px">(</font><font style="color:blue">object</font><font style="font-size:11px">)) { </font></td></tr><tr><td>                        ic.MoveCurrentTo(selectedItem); </td></tr><tr><td style="background-color:rgb(247, 247, 247)">                        lb.ScrollIntoView(selectedItem); </td></tr><tr><td>                    } </td></tr><tr><td style="background-color:rgb(247, 247, 247)">                }); </td></tr><tr><td> </td></tr><tr><td style="background-color:rgb(247, 247, 247)">            <font style="color:blue">if</font><font style="font-size:11px"> (val) data.CollectionChanged += autoscroller;   </font></td></tr><tr><td>            <font style="color:blue">else</font><font style="font-size:11px">  data.CollectionChanged -= autoscroller;  </font></td></tr><tr><td style="background-color:rgb(247, 247, 247)"> </td></tr><tr><td>        } </td></tr></tbody></table></div><br> <hr size="1" align="left" width="25%">Tamir <a href="http://blogs.microsoft.co.il/blogs/tamir">http://blogs.microsoft.co.il/blogs/tamir</a><br> <i>If your question was answered, please mark it.</i>Mon, 10 Nov 2008 13:25:53 Z2008-11-10T13:25:53Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/0f524459-b14e-4f9a-8264-267953418a2d#12e4143e-868e-48ec-94da-5c778d3e5dfehttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/0f524459-b14e-4f9a-8264-267953418a2d#12e4143e-868e-48ec-94da-5c778d3e5dfeBorka_http://social.msdn.microsoft.com/Profile/en-US/?user=Borka_Trivial ListBox/ListView Autoscroll Guys, please look at the title of my question - I was NOT asking HOW to do autoscroll - I know how to do it and I wrote how, for example, this can be done. I was looking for a TRIVIAL way to do it, and if there's no trivial way - a &quot;no&quot; is a perfectly good answer.<br><br>Meanwhile, the shortest, the simplest and pretty safe way I found is like this (a ~5 line, no extra classes solution):<br><br> <div style="border-right:#7f9db9 1px solid;border-top:#7f9db9 1px solid;font-size:11px;overflow:auto;border-left:#7f9db9 1px solid;line-height:100%! important;border-bottom:#7f9db9 1px solid;font-family:Courier New;background-color:white"> <table style="border-top-width:0px;border-left-width:0px;margin:2px 0px;width:99%;border-bottom:#eee 0px solid;border-collapse:collapse;background-color:#fff;border-right-width:0px" cellspacing=0 cellpadding=0> <colgroup> <col style="padding-left:10px;font-size:11px;border-bottom:#f7f7f7 1px solid;font-family:Courier New;white-space:nowrap"> <tbody> <tr> <td><font style="font-size:11px">lb.Items.SourceCollection.CollectionChanged += (s, e) =&gt; {  </font></td></tr> <tr> <td style="background-color:#f7f7f7">   var sv = </font><font style="color:green">/* find ScrollViewer in lb's visual tree */</font><font style="font-size:11px"> </font><font style="color:blue">as</font><font style="font-size:11px"> ScrollViewer;  </font></td></tr> <tr> <td>   sv.ScrollToEnd();  </td></tr> <tr> <td style="background-color:#f7f7f7">};  </td></tr> <tr> <td> </td></tr></tbody></table></div><br><br><font size=2> <p dir=ltr style="margin-right:0px"></font>Thanks a lot,<br>  Boris.<br></p>Mon, 10 Nov 2008 13:43:55 Z2008-11-10T13:43:55Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/0f524459-b14e-4f9a-8264-267953418a2d#23277ef7-dec8-4311-b23f-c68361d7e161http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/0f524459-b14e-4f9a-8264-267953418a2d#23277ef7-dec8-4311-b23f-c68361d7e161Tamir Khasonhttp://social.msdn.microsoft.com/Profile/en-US/?user=Tamir%20KhasonTrivial ListBox/ListView AutoscrollYou solution will work only for scroll to the end. In my solution you can scroll into newly added or last removed items. Also <font style="color:green">/* find ScrollViewer in lb's visual tree */</font> it a bit more, then ~5 lines of extra scroll and for sure at least one extra method (you're digging visual tree also, which is not very fast and efficient query) <hr size="1" align="left" width="25%">Tamir <a href="http://blogs.microsoft.co.il/blogs/tamir">http://blogs.microsoft.co.il/blogs/tamir</a><br> <i>If your question was answered, please mark it.</i>Mon, 10 Nov 2008 13:47:01 Z2008-11-10T13:47:01Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/0f524459-b14e-4f9a-8264-267953418a2d#af7e6961-dd8b-4d05-8e48-9f8e2c5308adhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/0f524459-b14e-4f9a-8264-267953418a2d#af7e6961-dd8b-4d05-8e48-9f8e2c5308adBorka_http://social.msdn.microsoft.com/Profile/en-US/?user=Borka_Trivial ListBox/ListView Autoscroll<p>In my question, I only want it to scroll to the end - that's generally the common meaning of autoscroll. I do not want it to do anything else. Moreover, I want it NOT to do <strong>anything</strong> else.<br><br>Anyway, it seems that nobody knows some really simple solution - I hope WPF will eventually have such trivial common tasks built-in.<br><br>Boris.<br></p>Mon, 10 Nov 2008 13:51:30 Z2008-11-10T13:51:30Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/0f524459-b14e-4f9a-8264-267953418a2d#4244fbc2-b8a2-4663-bf44-6dd5c2592dd5http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/0f524459-b14e-4f9a-8264-267953418a2d#4244fbc2-b8a2-4663-bf44-6dd5c2592dd5Alesandro Zamboninhttp://social.msdn.microsoft.com/Profile/en-US/?user=Alesandro%20ZamboninTrivial ListBox/ListView Autoscroll Hi,<br>i have resolved with this simple line of code.<br><br>...<br>mylistbox.SelectIndex(n); // Or SelectItem ...<br><font size=2> <p>mylistbox.ScrollIntoView( myobject );<br><br>After this the listbox show my new selection.<br><br>Alessandro</p></font>Tue, 02 Dec 2008 09:54:59 Z2008-12-02T09:54:59Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/0f524459-b14e-4f9a-8264-267953418a2d#ef813ce1-a1c7-4090-a574-64b85b12c958http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/0f524459-b14e-4f9a-8264-267953418a2d#ef813ce1-a1c7-4090-a574-64b85b12c958cvconcrkhttp://social.msdn.microsoft.com/Profile/en-US/?user=cvconcrkTrivial ListBox/ListView Autoscroll<p>Hello, I was just working with a ListBox that I dynamically add items (status text) to during a file import procedure.  I tried using ScrollIntoView(object item) to &quot;autoscroll&quot; down to the newly added item, but my display would end up lagging/freezing when displaying a large number of status messages (~50,000).  Not too surprising, considering it takes in an object instead of an index as a parameter.  This is the solution I came up with:</p> <p>xaml:</p> <div style="color:black;background-color:white"> <pre>&lt;ListBox x:Name=<span style="color:#a31515">&quot;m_cStatusList&quot;</span> ScrollViewer.ScrollChanged=<span style="color:#a31515">&quot;m_cStatusList_ScrollChanged&quot;</span>&gt;&lt;/ListBox&gt; </pre> </div> <p>cs:</p> <div style="color:black;background-color:white"> <pre><span style="color:blue">private</span> <span style="color:blue">void</span> m_cStatusList_ScrollChanged(<span style="color:blue">object</span> sender,ScrollChangedEventArgs e) { <span style="color:blue"> if</span> (e.ExtentHeightChange&gt;0.0) ((ScrollViewer)e.OriginalSource).ScrollToEnd(); } </pre> </div> <p>I haven't tried it with a bound source, but I'm assuming it would still work.<br/><br/>-Al</p>Tue, 21 Apr 2009 01:30:19 Z2009-04-21T01:31:54Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/0f524459-b14e-4f9a-8264-267953418a2d#5a90ba65-c217-41af-bc4b-f23dc57940c9http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/0f524459-b14e-4f9a-8264-267953418a2d#5a90ba65-c217-41af-bc4b-f23dc57940c9Jeffrey Knighthttp://social.msdn.microsoft.com/Profile/en-US/?user=Jeffrey%20KnightTrivial ListBox/ListView AutoscrollTry this:<br/> <br/> rather than <strong>Adding</strong> to your ObservableCollection, <strong>Insert(0, ...)</strong> instead.<br/> <br/> Combine that with  FlowDirection and see if it doesn't give you the effect you're looking for.<br/> <br/><hr class="sig">Jeffrey Knight | MCTS .NET 2.0 | RHCT | http://www.rootsilver.comWed, 29 Apr 2009 02:51:55 Z2009-04-29T02:51:55Z