WPF - reset ListBox scroll position when ItemsSource changes

前端 未结 5 2387
有刺的猬
有刺的猬 2021-02-18 23:41

I currently have a ListBox whose ItemsSource collection is bound to a property on my viewmodel, of type IEnumerable. When that preoprty\'s reference changes, the ListBox update

5条回答
  •  走了就别回头了
    2021-02-19 00:06

    Late answer:

    A simple solution is to add an event handler for the TargetUpdated event, and set NotifyOnTargetUpdated=True on the ItemsSource binding:

    
    

    and in the event handler, scroll to the top item:

    private void ListBox_TargetUpdated(object sender, DataTransferEventArgs e)
    {
        if (listBox.Items.Count > 0)
        {
            listBox.ScrollIntoView(listBox.Items[0]);
        }
    }
    

提交回复
热议问题