在WPF中更新ListBox控件的问题

时间:2010-10-09 18:44:28

标签: c# wpf listbox dispatcher

我有一个应用程序,它必须监视211个杆,并且每5秒钟它将更新2个ListBox控件,每个控件包含插入的杆或移除的杆。当我手动使用按钮插入/移除杆时,代码执行完美,ListBoxes正确更新。当我使用全局按钮插入所有211时,其中一个ListBox控件停止正常工作。

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);
                }
            }
        }

插入按钮的代码(删除一个类似)

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
            }
        }

全局按钮代码

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);
        }

当我按下全局按钮时,移除的杆列表框将按原样使用所有杆,插入的杆列表框将包含在按下按钮之前插入的杆。就好像按下此按钮时,此控件不会更新。附:如果我使用按钮或插入手动移除杆,它可以很好地工作。

关于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() + ")");
    }

更新:我已将更新ListBox代码放在一个单独的函数中,并采用了Marko的建议,并在函数中添加了InsertRods。现在一切正常,但似乎在我按下“紧急”按钮后,InsertedRods ListBox更新并且工作正常但RemovedRods只是停止更新,除非我手动执行(它应该通过Tick事件每5秒更新一次)。我甚至尝试插入所有的棒,更新ListBoxes并清除“有缺陷的”ListBox,但仍然没有,同样的结果。

1 个答案:

答案 0 :(得分:1)

我只是快速浏览一下你发布的代码,而没有深入关注它,并且想到了几个问题:

1)您发布了ListBox更新的代码,但是从其他两个代码块中不清楚您在哪里调用ListBox更新方法?

2)您为“插入按钮”发布的代码看起来更像是“删除按钮”中的代码,因为Removing_Rods.Add()...但是为什么要复制插入/删除按钮代码全局按钮代码?为什么没有插入方法,插入按钮和全局(插入)按钮调用?删除相同。如果您需要根据调用者是插入按钮还是全局按钮稍微改变代码,您可以传入一个变量并在insert方法中检查它。

3)您是否尝试过调试代码?与在执行全局按钮代码时是否调用列表框更新方法一样......