简体   繁体   English

在WPF中更新ListBox控件的问题

[英]Problem with updating ListBox control in WPF

I have an application which has to monitor 211 rods, and every 5 seconds it will update 2 ListBox controls, each one containing either the inserted rods or the removed ones. 我有一个必须监视211个杆的应用程序,每5秒钟它将更新2个ListBox控件,每个控件都包含插入的杆或已移除的杆。 When I manually use the button for inserting/removing rods the code executes perfectly and the ListBoxes update properly. 当我手动使用按钮来插入/移除杆时,代码会完美执行,并且ListBoxes会正确更新。 When I use the global button which inserts all 211 one of the ListBox controls stops working properly. 当我使用插入所有211的全局按钮时,ListBox控件之一停止正常工作。

The code for ListBox update ListBox更新的代码

bool IClear = true, RClear = true;
        for (int foo = 0; foo < Rods.Count; foo++)
        {
            if (Rods[foo].State == RodState.Inserted)
            {
                UpdateRodList update = new UpdateRodList(UpdateIRodUI);
                if (IClear)
                {
                    InsertedRods.Dispatcher.BeginInvoke(update, System.Windows.Threading.DispatcherPriority.Normal, foo, true);
                    IClear = false;
                }
                else
                {
                    InsertedRods.Dispatcher.BeginInvoke(update, System.Windows.Threading.DispatcherPriority.Normal, foo, false);
                }
            }
            if (Rods[foo].State == RodState.Removed)
            {
                UpdateRodList update = new UpdateRodList(UpdateRRodUI);
                if (RClear)
                {
                    RemovedRods.Dispatcher.BeginInvoke(update, System.Windows.Threading.DispatcherPriority.Normal, foo, true);
                    RClear = false;
                }
                else
                {
                    RemovedRods.Dispatcher.BeginInvoke(update, System.Windows.Threading.DispatcherPriority.Normal, foo, false);
                }
            }
        }

The code for the insert button (the remove one is similar) 插入按钮的代码(删除按钮类似)

Int32[] RodsID = null;
        bool bParsed = false;
        if (RemovingRods_.Text.Contains("*"))
        {
            RodsID = new Int32[211];
            for (int i = 0; i < 211; i++)
            {
                RodsID[i] = i;
            }
            RemovingRods_.Text = "";
            bParsed = true;
        }
        if (RemovingRods_.Text.Contains("-"))
        {
            string stext = RemovingRods_.Text;
            Int32 a = Int32.Parse(RemovingRods_.Text.Substring(0, RemovingRods_.Text.IndexOf("-")));
            Int32 b = Int32.Parse(RemovingRods_.Text.Substring(RemovingRods_.Text.IndexOf("-") + 1));
            RodsID = new Int32[b - a];
            for (int i = 0; i < b - a; i++)
            {
                RodsID[i] = i + a;
            }
            RemovingRods_.Text = "";
            bParsed = true;
        }
        if (!bParsed)
        {
            string[] RodsID_;
            char[] split = { ' ' };
            RodsID_ = RemovingRods_.Text.Split(split);
            RemovingRods_.Text = "";
            RodsID = new Int32[RodsID_.Length];
            for (int i = 0; i < RodsID_.Length; i++)
            {
                RodsID[i] = Int32.Parse(RodsID_[i]);
            }
        }
        foreach (int numb in RodsID)
        {
            if (Rods[numb].Type == "Control Rod")
            {
                ControlRod Rod = new ControlRod();
                Rod.Number = numb;
                Rod.RodState = RodState.Changing;
                RemovingCRods.Add(Rod);
            }
            if (Rods[numb].Type == "Shortened Control Rod")
            {
                ShortenedControlRod Rod = new ShortenedControlRod();
                Rod.Number = numb;
                Rod.RodState = RodState.Changing;
                RemovingSRods.Add(Rod);
            }
            if (Rods[numb].Type == "Automated Control Rod")
            {
                // Automated Rods -- NO MANUAL CONTROL
            }
        }

And the global button code 和全局按钮代码

try
        {
            Int32[] RodsID = null;
            string text = "0-211";
            RodsID = new Int32[211];
            for (int i = 0; i < 211; i++)
            {
                RodsID[i] = i;
            }
            foreach (int numb in RodsID)
            {
                if (Rods[numb].Type == "Control Rod")
                {
                    ControlRod Rod = new ControlRod();
                    Rod.Number = numb;
                    Rod.RodState = RodState.Changing;
                    InsertingCRods.Add(Rod);
                }
                if (Rods[numb].Type == "Shortened Control Rod")
                {
                    ShortenedControlRod Rod = new ShortenedControlRod();
                    Rod.Number = numb;
                    Rod.RodState = RodState.Changing;
                    InsertingSRods.Add(Rod);
                }
                if (Rods[numb].Type == "Automated Control Rod")
                {
                    AutomatedControlRod Rod = new AutomatedControlRod();
                    Rod.Number = numb;
                    Rod.RodState = RodState.Changing;
                    InsertingARods.Add(Rod);
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

What happens is when I press the global one, the Removed Rods ListBox will have all the rods as it should, and the Inserted Rods ListBox will contain the rods that were inserted before I pressed the button. 发生的是,当我按下全局杆时,“移除的杆列表框”将具有所有应有的杆,并且“插入杆列表框”将包含在按下按钮之前插入的杆。 It's as if when I pressed this button, this control doesn't update. 好像当我按下此按钮时,此控件不会更新。 PS If I remove rods manually using the button or insert it works perfectly. PS:如果我使用按钮手动移除杆或将其插入,效果很好。

As for the code requested by Marko: 至于Marko要求的代码:

 private void UpdateIRodUI(Int32 foo, Boolean clear)
    {
        if (clear)
        {
            InsertedRods.Items.Clear();
        }
        InsertedRods.Items.Add(Rods[foo].Number + " : " + Rods[foo].Type + " (" + foo.ToString() + ")");
    }

    private void UpdateRRodUI(Int32 foo, Boolean clear)
    {
        if (clear)
        {
            RemovedRods.Items.Clear();
        }
        RemovedRods.Items.Add(Rods[foo].Number + " : " + Rods[foo].Type + " (" + foo.ToString() + ")");
    }

Update: I have put the update ListBox code in a seperate function and took Marko's advice and also put in a function the InsertRods. 更新:我已经将更新的ListBox代码放在一个单独的函数中,并听取了Marko的建议,还将一个函数插入了InsertRods。 Everything works fine now, but it seems that after I press the "emergency" button the InsertedRods ListBox updates and works just fine but RemovedRods just stops updating, unless I do it manually (it's supposed to update every 5 seconds through a Tick event). 现在一切正常,但是似乎在我按下“紧急”按钮之后,InsertedRods ListBox会更新并正常工作,但是RemovedRods会停止更新,除非我手动进行(应该通过Tick事件每5秒更新一次)。 I even tried inserting all the rods, updating the ListBoxes and the clearing the "faulty" ListBox and still nothing, same result. 我什至尝试插入所有杆,更新ListBoxes并清除“有问题的” ListBox,仍然没有结果,结果相同。

I just took a quick glance of your posted code without focusing very deeply on it and a couple of questions popped into mind: 我只是快速浏览了一下您发布的代码,而没有十分关注它,所以想到了两个问题:

1) You posted your code for ListBox update, but it's unclear from the other two code pieces that where do you call the ListBox update method? 1)您发布了用于ListBox更新的代码,但是从其他两个代码段中不清楚您在哪里调用ListBox更新方法?

2) The code that you posted for "insert button" looks more like the code from the "remove button", because of Removing_Rods.Add()... But why do you duplicate your insert/remove button code in your global button code? 2)由于Remove_Rods.Add(),您为“插入按钮”发布的代码更像是“删除按钮”中的代码,但是为什么要在全局按钮代码中重复插入/删除按钮代码? Why not have an insert method, that both the insert button and global (insert) button call? 为什么没有插入方法,即同时调用插入按钮和全局(插入)按钮? And the same for remove. 和删除相同。 If you need to slightly alter the code based on whether the caller is the insert button or the global button, you can pass in a variable and check it inside the insert method. 如果您需要根据调用者是插入按钮还是全局按钮来稍微更改代码,则可以传入一个变量,然后在insert方法内部对其进行检查。

3) Have you tried debugging your code? 3)您是否尝试过调试代码? As in whether the listbox update method is called when the global button code is executed... 就像在执行全局按钮代码时是否调用列表框更新方法一样...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM