c# – 当listview刷新时,焦点不是“保持”

我目前在我的代码中遇到问题,因为对特定项目行的关注不会成立.我创建了int focusReference以跟踪当前聚焦的行,但它似乎在每几秒后重置为0,即一旦用户选择一行,在几秒钟内,它“忘记”用户选择.我在下面列举了一个例子:

从下面的代码中可以看出,我每隔几秒钟就会有一个计时器.我已经被告知每隔几秒刷一次我的清单就会导致每隔几秒就失去焦点.

如何以编程方式将焦点设置在项目上,以保留用户的焦点?我试图实现一个FocusItem方法但它似乎没有工作.这是我的程序非常重要的一个方面,我需要它才能正常工作,否则其他功能(如我正在实现的右键单击上下文菜单)也将无效:

public MainWindow()
{
    InitializeComponent();
    int focusReference = 0;
    PlotListView.SelectionChanged += (s, ee) => { PlotListView_SelectionChanged(s, ee, focusReference); };
    var dbObject = new DbConnect();
    dbObject.OpenConnection();
    RefreshPlotTimer(filterReference, focusReference);
}

public void PlotListView_SelectionChanged(object sender, SelectionChangedEventArgs e, int focusReference)
{
    if (PlotListView.SelectedItems.Count == 0) return;
    var selectedItem = (DbConnect.PlotList)PlotListView.SelectedItems[0];
    focusReference = Convert.ToInt32(selectedItem.PlotId);
    FocusItem(focusReference);
}

private void FocusItem(int focusReference)
{
    if (PlotListView.SelectedItems.Count != 0)
    {
        DbConnect.PlotList plotList =
            PlotListView.Items.OfType<DbConnect.PlotList>()
            .FirstOrDefault(p => Convert.ToInt32(p.PlotId) == focusReference);
        if (plotList != null)
        {
            //get visual container
            var container = PlotListView.ItemContainerGenerator.ContainerFromItem(plotList) as ListViewItem;
            if (container != null)
            {
                container.IsSelected = true;
                container.Focus();
            }
        }
    }
}

public void RefreshPlotTimer(int filterReference, int focusReference)
{
    var refreshTimer = new Timer();
    refreshTimer.Elapsed += (sender, e) => RefreshPlot(sender, e, filterReference, focusReference);
    refreshTimer.Interval = 2500;
    refreshTimer.Enabled = true;
}

public void RefreshPlot(object source, ElapsedEventArgs e, int filterReference, int focusReference)
{
    var dbObject = new DbConnect();
    dbObject.OpenConnection();
    dbObject.RefreshPlot();
    Dispatcher.Invoke(() =>
    {
        FocusItem(focusReference);
        if (!string.IsNullOrWhiteSpace(FilterTextBox.Text) &&
            (!Regex.IsMatch(FilterTextBox.Text, "[^0-9]")))
        {
            filterReference = Convert.ToInt32(FilterTextBox.Text);
        }
    });
    ResetPlot(filterReference);
}

public void ResetPlot(int filterReference)
{
    var dbObject = new DbConnect();
    dbObject.OpenConnection();

    List<DbConnect.PlotList> plotList = dbObject.SelectPlotLists(filterReference);
    Dispatcher.BeginInvoke(
        new ThreadStart(() => PlotListView.ItemsSource = plotList));
    int jobSum = 0;
    int bidSum = 0;
    foreach (DbConnect.PlotList item in PlotListView.Items)
    {
        jobSum += Convert.ToInt32(item.Jobs);
        bidSum += Convert.ToInt32(item.Bids);
    }

    Dispatcher.BeginInvoke(
        new ThreadStart(() => JobBidRatioTextBlock.Text = jobSum + " jobs - " + bidSum + " bids"));
}

更新2

我回到了我最初使用的代码,可能是FocusItem方法没有保留listview项焦点吗?

private void FocusItem(int focusReference)
{
    Dispatcher.Invoke(() =>
    {
        if (PlotListView.SelectedItems.Count != 0)
        {
            DbConnect.PlotList plotList =
                PlotListView.Items.OfType<DbConnect.PlotList>()
                    .FirstOrDefault(p => Convert.ToInt32(p.PlotId) == focusReference);
            if (plotList != null)
            {
                //get visual container
                var container = PlotListView.ItemContainerGenerator.ContainerFromItem(plotList) as ListViewItem;
                if (container != null)
                {
                    container.IsSelected = true;
                    container.Focus();
                }
            }
        }
    }
}

根据控制台中的内容,我在正确的位置调用它并且索引绝对是正确的.

PlotListView_SelectionChanged 4
Before refresh 4
After refresh 4
PlotListView_SelectionChanged 7
Before refresh 7
After refresh 7

Before refresh 7
After refresh 7

两者都在刷新绘图方法…

public void RefreshPlot(object source, ElapsedEventArgs e)
{
    var dbObject = new DbConnect();
    dbObject.OpenConnection();
    dbObject.RefreshPlot();
    Console.WriteLine("\rBefore refresh " + focusReference);
    Dispatcher.Invoke(() =>
    {
        if (!string.IsNullOrWhiteSpace(FilterTextBox.Text) &&
            (!Regex.IsMatch(FilterTextBox.Text, "[^0-9]")))
        {
            filterReference = Convert.ToInt32(FilterTextBox.Text);
        }
    });
    ResetPlot(filterReference);
    Console.WriteLine("After refresh " + focusReference);
    FocusItem(focusReference);
}

并重置绘图方法……

public void ResetPlot(int filterReference)
{
    // Establish MySQL connection
    var dbObject = new DbConnect();
    dbObject.OpenConnection();

    // Fill plot list view
    List<DbConnect.PlotList> plotList = dbObject.SelectPlotLists(filterReference);
    Dispatcher.BeginInvoke(
        new ThreadStart(() => PlotListView.ItemsSource = plotList));
    int jobSum = 0;
    int bidSum = 0;
    foreach (DbConnect.PlotList item in PlotListView.Items)
    {
        jobSum += Convert.ToInt32(item.Jobs);
        bidSum += Convert.ToInt32(item.Bids);
    }
    FocusItem(focusReference);

    // Determine job/bid list ratio
    Dispatcher.BeginInvoke(
        new ThreadStart(() => JobBidRatioTextBlock.Text = jobSum + " jobs - " + bidSum + " bids"));
}

最佳答案 在进行刷新之前,您需要记住SelectedItem并在刷新后将其设置为agian.这就是理论.

如果是对象,您需要找到哪个对象现在与之前的对象相同(因为它不再是同一个实例),因此您必须在新结果列表中搜索它并将其分配给SelectedItem.

最简单的方法是重新设置SelectedIndex,但由于列表可能会更改,因此迟早会重新选择错误的项目.

// Gets current selection.
public DbConnect.PlotList SelectedPlotList
{
    get
    {
        return PlotListView.SelectedItem as DbConnect.PlotList;
    }
}
public void ResetPlot(int filterReference)
{
    // Get current plot number;
    int? plotNumber = SelectedPlotList == null ? (int?)null : SelectedPlotList.PlotNumber;

    var dbObject = new DbConnect();
    dbObject.OpenConnection();

    List<DbConnect.PlotList> plotList = dbObject.SelectPlotLists(filterReference);

    // Find the plot list in the new list.  
    DbConnect.PlotList selectPlotList = 
        plotNumber.HasValue
        ? plotList.Where(x => x.PlotNumber == plotNumber.Value).FirstOrDefault();
        : null;
    Dispatcher.BeginInvoke(new ThreadStart(() => PlotListView.ItemsSource = plotList));

    // Re-select plot list if found in the new list.
    if (selectPlotList != null)
    {
        PlotListView.SelectedItem = selectPlotList;
    }
    int jobSum = 0;
    int bidSum = 0;


    foreach (DbConnect.PlotList item in PlotListView.Items)
    {
        jobSum += Convert.ToInt32(item.Jobs);
        bidSum += Convert.ToInt32(item.Bids);
    }

    Dispatcher.BeginInvoke(
        new ThreadStart(() => JobBidRatioTextBlock.Text = jobSum + " jobs - " + bidSum + " bids"));
}

编辑:

我刚刚测试过一个快速的&脏的演示,它的工作原理.其他地方肯定有bug.

这会生成7个具有唯一ID的项目,如果再次绘制,则重新选择最后选择的项目:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        List<ListViewItem> items = new List<ListViewItem>();
        Random rnd = new Random(DateTime.Now.Millisecond);
        HashSet<int> ids = new HashSet<int>();
        for (int i = 0; i < 7; i++)
        {
            int id = 0;
            do
            {
                id = rnd.Next(0, 10);
            } while (ids.Contains(id));
            ids.Add(id);
            items.Add(new ListViewItem() { Id = id, Name = "Item-" + i });
        }
        int? selectedId = listView1.SelectedItem != null ? (listView1.SelectedItem as ListViewItem).Id : (int?)null;
        listView1.ItemsSource = items;

        if (selectedId.HasValue)
        {
            listView1.SelectedItem = items.Where(x => x.Id == selectedId).FirstOrDefault();
            listView1.Focus();
        }
    }
}

class ListViewItem
{
    public int Id { get; set; }
    public string Name { get; set; }
}
点赞