c#wpf在Task结束时如何冻结ListBox

问题描述 投票:0回答:1

如何冻结ListBox直到Thread结束,以防止SelectedIndex突出损失?

这是我的实际代码(但是当我禁用并启用ListBox时,选择突出显示会丢失):

        ListBox_DeviceList.IsEnabled = false;


        if (ListBox_DeviceList.SelectedItem != null)
        {
            bool error = await GetAvailableJobs();

            //enables when Task ends ...
            ListBox_DeviceList.IsEnabled = true;

            if (error)
            {
                //do something
            }
            else
            {
                //do something
            }
        }
wpf listbox freeze
1个回答
0
投票

我认为您需要将Enable属性(使用.IsEnabled)设置为false,最后设置为true。

ListBox_DeviceList.Enabled = false;


    if (ListBox_DeviceList.SelectedItem != null)
    {
        bool error = await GetAvailableJobs();

        //enables when Task ends ...
        ListBox_DeviceList.Enabled = true;

        if (error)
        {
            //do something
        }
        else
        {
            //do something
        }
    }

其他方式,是调用SuspendLayout()和ResumeLayout()方法。

ListBox_DeviceList.SuspendLayout();


    if (ListBox_DeviceList.SelectedItem != null)
    {
        bool error = await GetAvailableJobs();

        //enables when Task ends ...
        ListBox_DeviceList.ResumeLayout();

        if (error)
        {
            //do something
        }
        else
        {
            //do something
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.