我似乎无法弄清楚,因为我似乎无法将 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,但它也适用于 ListView
至于不使用 Focus,请发布您如何使用 ScrollIntoView。
关于c# - 如何让 ListView 专注于特定项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17068314/