C# 调用Combobox并在wpf c中使用它#

C# 调用Combobox并在wpf c中使用它#,c#,wpf,multithreading,combobox,C#,Wpf,Multithreading,Combobox,我试图调用combobox来填充它的ItemSource,并在调用期间使用它的项源,但在完成调用之前我无法使用它,我希望在调用期间使用它。我在Gui中的控件名是cb\u attach。我的参考控件是tempatachcombobox private void cb_attach_Loaded(object sender, System.Windows.RoutedEventArgs e) { tempattachcombobox = sender as ComboBox; Thr

我试图调用combobox来填充它的
ItemSource
,并在调用期间使用它的项源,但在完成调用之前我无法使用它,我希望在调用期间使用它。我在Gui中的控件名是
cb\u attach
。我的参考控件是
tempatachcombobox

private void cb_attach_Loaded(object sender, System.Windows.RoutedEventArgs e)
{
    tempattachcombobox = sender as ComboBox;
    Thread UpdateComboboxThread = new Thread(UpdateCombobox);
    UpdateComboboxThread.Start();
}

private void UpdateCombobox()
{
    while (GlobalV.Working == true)
    {
        int tempselectedindex = -1;
        tempattachcombobox.Dispatcher.Invoke(
        DispatcherPriority.Normal,
        new Action(delegate() { tempselectedindex = tempattachcombobox.SelectedIndex; }));
        List<int> numberList = Enumerable.Range(0, GlobalV.Attatched_Elements.Count).ToList();
        tempattachcombobox.Dispatcher.Invoke(
        DispatcherPriority.Normal,
        new Action(delegate()
        {
            tempattachcombobox.ItemsSource = numberList;
            tempattachcombobox.SelectedIndex = tempselectedindex;
        }));
    }

    MessageBox.Show("Process Completed!", "Process Status", MessageBoxButton.OK, MessageBoxImage.Information);
}

private void cb_attach_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
    if (drawattach == false)
    {
        try
        {
            real_area.DrawingArea.Children.Clear();
            real_area.DrawGrid(25);
            real_area.DrawElement(GlobalV.Current_Elements[0]);
            real_area.DrawElement_Using_Points(GlobalV.Current_Elements[1], cb_attach.SelectedIndex);
        }
        catch { }
    }
    else
    {
        try
        {
            real_area.DrawingArea.Children.Clear();
            real_area.DrawGrid(25);
            real_area.DrawElement(GlobalV.Attatched_Elements[cb_attach.SelectedIndex]);
            real_area.DrawHash(GlobalV.Attatched_Elements[cb_attach.SelectedIndex]);
        }
        catch { }
    }
}
private void cb\u attach\u加载(对象发送方,System.Windows.RoutedEventArgs e)
{
tempattachcombobox=发送方作为ComboBox;
Thread UpdateComboxThread=新线程(UpdateComboxThread);
updateComboxThread.Start();
}
私有void updatembobox()
{
while(GlobalV.Working==true)
{
int tempselectedindex=-1;
tempattachcombobox.Dispatcher.Invoke(
调度员优先权,正常,
新操作(委托(){tempselectedindex=tempattachcombobox.SelectedIndex;}));
List numberList=Enumerable.Range(0,GlobalV.attatted_Elements.Count).ToList();
tempattachcombobox.Dispatcher.Invoke(
调度员优先权,正常,
新操作(委托()
{
tempattachcombobox.ItemsSource=numberList;
tempattachcombobox.SelectedIndex=tempselectedindex;
}));
}
MessageBox.Show(“进程完成!”,“进程状态”,MessageBoxButton.OK,MessageBoxImage.Information);
}
私有无效cb\U附加\U选择更改(对象发送方,System.Windows.Controls.SelectionChangedEventArgs e)
{
如果(drawattach==false)
{
尝试
{
real_area.DrawingArea.Children.Clear();
真实面积。绘图网格(25);
实面积元素(全局当前元素[0]);
使用点(全局当前元素[1],cb\u-attach.SelectedIndex)的实面积;
}
捕获{}
}
其他的
{
尝试
{
real_area.DrawingArea.Children.Clear();
真实面积。绘图网格(25);
real_area.DrainElement(全局附加元素[cb_附加.SelectedIndex]);
real_area.DrawHash(GlobalV.attached_元素[cb_attach.SelectedIndex]);
}
捕获{}
}
}

realarea在主GUI线程上工作

,因为GlobalV.attattached_Elements.Count在另一个线程中更改是的,为什么不将您的组合框绑定到一个(可观察的)列表?把你的多线程技能放在重要的地方,而不是用项目填充一个好的旧组合框…+1。我正在查看这段代码,并看到一些潜在的问题。首先存储tempatachComboBox的一个局部变量,然后在后台线程中使用它(如果线程同时运行多次,它将不起作用),其次是Dispatcher.Invoke是一个对UI线程的同步封送,它会阻止线程池线程,基本上降低了它作为多线程解决方案的有效性。你能发布更多关于你想要达到的目标的细节吗?代码示例很有用,但多了解一些背景知识也会有所帮助。致以最良好的问候,请同意文森特·皮尔的观点。后台的标准用法是获取昂贵的数据,然后在主UI线程上绑定这些数据。通过设计,只有创建到UI对象的线程才能访问UI对象。是的,你可以尝试欺骗那个模特,但为什么呢?