c# - 如何让 ListView 专注于特定项目?

标签 c# wpf listview gridview collections

我似乎无法弄清楚,因为我似乎无法将 ListView 的项目转换为 ListViewItem 类型并调用 ListViewItem.Focus()。以下内容将不起作用,因为 ListView 的项目是 LogRecord 类型:

((ListViewItem)listView.Items[0]).Focus();

编辑:我希望滚动条移动到该项目所在的位置,或者更好地说,该项目在用户看到的项目列表中变得可见。

关于如何让我的 ListView 专注于特定项目有什么想法吗?现在它绑定(bind)到一个集合。以下是我设置 ListView 对象的方法:

listView = new ListView();
Grid.SetRow(listView, 1);
grid.Children.Add(listView);
GridView myGridView = new GridView();
// Skipping some code here to set up the GridView columns and such.
listView.View = myGridView;
Binding myBinding = new Binding();
myBinding.Source = PaneDataContext.LogsWrapper;
listView.SetBinding(ItemsControl.ItemsSourceProperty, myBinding);

我绑定(bind)到此数据类型(LogRecord 包含 LogRecord.Message 等内容,对应于 GridView 上的消息列等;并且代码有效):

        public class LogRecordWrapper : IEnumerable<LogRecord>, INotifyCollectionChanged
        {
            public List<LogRecord> RowList { get; set; }

            public event NotifyCollectionChangedEventHandler CollectionChanged;

            public LogRecordWrapper()
            {
                RowList = new List<LogRecord>();
            }

            protected virtual void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
            {
                if (CollectionChanged != null)
                {
                    CollectionChanged(this, e);
                }
            }

            public void SignalReset()
            {
                OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset, null));
            }

            public void Add(LogRecord item)
            {
                RowList.Add(item);
                OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item));
            }

            public IEnumerator<LogRecord> GetEnumerator()
            {
                return RowList.GetEnumerator();
            }

            System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
            {
                return GetEnumerator();
            }
        }

最佳答案

ListView.ScrollIntoView

ListBox.ScrollIntoView Method

该链接显示 ListBox,但它也适用于 ListView

至于不使用 Focus,请发布您如何使用 ScrollIntoView。

关于c# - 如何让 ListView 专注于特定项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17068314/

相关文章:

c# - 如何通过不同时区移动本地 DateTime 值

c# - 在 Unity 中序列化多个变量的更简洁方法

wpf - 如何在标签值更改时设置背景颜色动画

c# - 如何在c#.net中的多线程和每行一个线程中逐行处理文件?

Android 可点击 ListView

c# - MediatR NotificationHandler 即发即忘

c# - 使用 Rx 同步异步事件

wpf - NotifyOnValidationError如何最终调用CanExecute(以及为什么它不适用于MVVM Light RelayCommand)

c# - Linq group by 创建嵌套 ListView

android - 如何在android中创建六边形 ListView