簡體   English   中英

在WPF中更新ListBox控件的問題

[英]Problem with updating ListBox control in WPF

我有一個必須監視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);
        }

發生的是,當我按下全局桿時,“移除的桿列表框”將具有所有應有的桿,並且“插入桿列表框”將包含在按下按鈕之前插入的桿。 好像當我按下此按鈕時,此控件不會更新。 PS:如果我使用按鈕手動移除桿或將其插入,效果很好。

至於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)您發布了用於ListBox更新的代碼,但是從其他兩個代碼段中不清楚您在哪里調用ListBox更新方法?

2)由於Remove_Rods.Add(),您為“插入按鈕”發布的代碼更像是“刪除按鈕”中的代碼,但是為什么要在全局按鈕代碼中重復插入/刪除按鈕代碼? 為什么沒有插入方法,即同時調用插入按鈕和全局(插入)按鈕? 和刪除相同。 如果您需要根據調用者是插入按鈕還是全局按鈕來稍微更改代碼,則可以傳入一個變量,然后在insert方法內部對其進行檢查。

3)您是否嘗試過調試代碼? 就像在執行全局按鈕代碼時是否調用列表框更新方法一樣...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM