2013-06-12 25 views
1

我似乎无法弄清楚,因为我似乎无法将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(); 
      } 
     } 
+2

ListView.ScrollIntoView http://msdn.microsoft.com/en-us/library/system.windows.controls .listbox.scrollintoview.aspx – Paparazzi

+0

@Blam - 非常感谢。救了我。请将此解决方案作为答案发布,以便我可以接受。 – Alexandru

+0

@Blam - 如果ScrollView目前不是焦点元素的一部分?这似乎并不实际执行此操作,除非滚动视图是重点元素的一部分。有什么办法可以解决这个问题吗? – Alexandru

回答

1

ListView.ScrollIntoView

ListBox.ScrollIntoView Method

该链接说的是ListBox,但它也适用于ListView也

至于没有与Focus一起工作请发表您如何使用ScrollIntoView。

+0

我正在使用Telerik的RadPane控件。它的对接工具就像你在Visual Studio中拥有对接和窗格一样。我将我的listView添加到RadPane中,并且ScrollIntoView将在RadPanes上可见,但对于选项卡上的RadPanes不会起作用,奇怪。 – Alexandru

+0

我知道肯定它调用的方法与正确的项目,但是当RadPane选项卡,不可见或固定和不可见,它只是不会滚动到视图中... – Alexandru

+0

我找到了一种方法来解决它通过将窗格设置为布尔值来表示它是否需要同步到某个记录,然后连接到活动窗格更改事件处理程序。 – Alexandru

0

你可以使用:

listView.Items[0].Focused = true; 

...或者是:

listVIew.Items[0].Selected = true; 

(我还不能肯定是什么样的 “焦点” 你以后)

然后用结合(或地方使用):

listView.Items[0].EnsureVisible(); 
+0

我希望滚动条移动到项目所在的位置,基本上或更好地说,项目在用户看到的项目列表中可见。 – Alexandru

+0

但是... listView.Items [0]没有聚焦或选择或EnsureVisible,因为它们不是ListViewItem类型,因为数据绑定到我的LogRecord集合。 – Alexandru

+0

我最初的问题是数据绑定到不是ListViewItem类型的集合的情况,所以现在如何确保项目可见? – Alexandru

0

这太神奇了! 05-29-2002,04:53 PM#1 Jim Guest ListView EnsureVisible无法正常工作。有任何想法吗? 代码...实际上找到并突出显示选定的项目,它只是不滚动 并使其可见。用户被迫滚动到该项目。

2004年4月4日,上午12时07 luchmun 嗨, 窗体上的工作......一切顺利,但问题是,即使我使用lstitem.ensurevisible与listitem.selected = TRUE当前条目不会变得可见。

11年过去了,它仍然不起作用,没有人,连微软似乎都不知道为什么? 对我有用的答案是listview1.ensurevisible(itemindex)NOT listview.items(itemindex).ensurevisible

相关问题