How can I have a ListBox auto-scroll when a new item is added?

后端 未结 12 2371
走了就别回头了
走了就别回头了 2020-11-28 20:34

I have a WPF ListBox that is set to scroll horizontally. The ItemsSource is bound to an ObservableCollection in my ViewModel class. Every time a new item is added, I want th

12条回答
  •  Happy的楠姐
    2020-11-28 20:58

    This works for me:

    DirectoryInfo di = new DirectoryInfo(folderBrowserDialog1.SelectedPath);
    foreach (var fi in di.GetFiles("*", SearchOption.AllDirectories))
    {
        int count = Convert.ToInt32(listBox1.Items.Count); // counts every listbox entry
        listBox1.Items.Add(count + " - " + fi.Name); // display entrys
        listBox1.TopIndex = count; // scroll to the last entry
    }
    

提交回复
热议问题