简体   繁体   English

如何使ListView专注于特定项目?

[英]How can I get my ListView to focus in on a particular item?

I can't seem to figure it out, because I don't seem to be able to cast my ListView's items to the ListViewItem type and call ListViewItem.Focus(). 我似乎无法弄清楚,因为我似乎无法将ListView的项目转换为ListViewItem类型并调用ListViewItem.Focus()。 The following won't work because the ListView's items are of type LogRecord: 由于ListView的项目类型为LogRecord,因此以下操作将不起作用:

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

EDIT: I want the scrollbar to move to where the item is, basically, or better said, that the item becomes visible in the list of items the user sees. 编辑:我希望滚动条移动到该项目所在的位置,或者说得更好,该项目在用户看到的项目列表中变得可见。

Any ideas on how I can get my ListView to focus in on a particular item? 关于如何使ListView专注于特定项目的任何想法? Right now its bound to a collection. 现在,它已绑定到一个集合。 Here's how I set up my ListView object: 这是我设置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);

I bind to this data type (LogRecord contains things like LogRecord.Message that corresponds to a Message column on the grid view, etc.; and the code works): 我绑定到此数据类型(LogRecord包含与网格视图上的Message列相对应的LogRecord.Message之类的东西;该代码有效):

        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 ListView.ScrollIntoView

ListBox.ScrollIntoView Method ListBox.ScrollIntoView方法

The link says ListBox but it works with ListView also 该链接显示为ListBox,但它也可以与ListView一起使用

As for not working with Focus please post how you are using ScrollIntoView. 至于不能使用Focus,请张贴您如何使用ScrollIntoView。

Could you use: 您可以使用:

listView.Items[0].Focused = true;

...or perhaps: ...也许:

listVIew.Items[0].Selected = true;

(I'm not positive what kind of "focus" you are after) (我不赞成你追求什么样的“重点”)

Then combine with (or use in place): 然后结合(或就地使用):

listView.Items[0].EnsureVisible();

This is amazing!!!! 这真太了不起了!!!! 05-29-2002, 04:53 PM #1 Jim Guest ListView EnsureVisible not working. 2002年5月29日,下午4:53#1 Jim Guest ListView确保无法正常工作。 Any ideas? 有任何想法吗? The code ... in fact find and highlight selected item, it's just not scrolling to it and making it visible. 该代码...实际上找到并突出显示了选定的项目,只是没有滚动到它并使其可见。 The user is forced to scroll to the item. 用户被迫滚动到该项目。

04-04-2004, 12:07 AM luchmun Hi, working on a form...Everything goes fine but the problem is that even i use the lstitem.ensurevisible with listitem.selected = true the current entry does not become visible. 2004年4月4日,上午12:07 luchmun嗨,正在处理一个表单...一切都很好,但是问题是,即使我将lstitem.ensurevisible与listitem.selected = true一起使用,当前条目也不可见。

11 YEARS later and still it does not work and no one, not even Microsoft seem to know why? 11年后,仍然无法正常运行,没有人,甚至Microsoft都不知道为什么? The answer that works for me is listview1.ensurevisible(itemindex) NOT listview.items(itemindex).ensurevisible 对我有用的答案是listview1.ensurevisible(itemindex)而不是listview.items(itemindex).ensurevisible

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM