我似乎无法弄清楚,因为我似乎无法将ListView的项目转换为ListViewItem类型并调用ListViewItem.Focus()。下面将不起作用,因为ListView的项目类型的LogRecord的:如何让我的ListView专注于特定项目?
((ListViewItem)listView.Items[0]).Focus();
编辑:我想滚动条说移动到该项目,基本上,或更好的,该项目在列表中变得可见用户看到的项目。
关于如何让我的ListView专注于特定项目的任何想法?现在它绑定到一个集合。下面是我如何设置我的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);
我绑定到该数据类型(包含的LogRecord之类的东西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 http://msdn.microsoft.com/en-us/library/system.windows.controls .listbox.scrollintoview.aspx – Paparazzi
@Blam - 非常感谢。救了我。请将此解决方案作为答案发布,以便我可以接受。 – Alexandru
@Blam - 如果ScrollView目前不是焦点元素的一部分?这似乎并不实际执行此操作,除非滚动视图是重点元素的一部分。有什么办法可以解决这个问题吗? – Alexandru