gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 13:40:55 25 4
gpt4 key购买 nike

我似乎无法弄清楚,因为我似乎无法将我的 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 上的 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

ListBox.ScrollIntoView Method

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

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

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com