WPF ListBox Scroll to end automatically

后端 未结 9 801
耶瑟儿~
耶瑟儿~ 2020-12-13 08:25

In my application, I have a ListBox with items. The application is written in WPF.

How can I scroll automatically to the last added item? I want the

9条回答
  •  无人及你
    2020-12-13 09:10

    For me, the simplest working way was this: (without Binding)

     private void WriteMessage(string message, Brush color, ListView lv)
            {
    
                Dispatcher.BeginInvoke(new Action(delegate
                {
                    ListViewItem ls = new ListViewItem
                    {
                        Foreground = color,
                        Content = message
                    };
                    lv.Items.Add(ls);
                    lv.ScrollIntoView(lv.Items[lv.Items.Count - 1]);
                }));
            }
    

    Don't need to create classes or change the xaml, just write the messages with this method and it scroll automatically.

    Calling just

    myLv.Items.Add(ls);
    myLv.ScrollIntoView(lv.Items[lv.Items.Count - 1]);
    

    for exemple, don't work for me.

提交回复
热议问题