Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 自动完成框-填充中的值不一致_C#_Wpf_Autocomplete_Wpftoolkit - Fatal编程技术网

C# 自动完成框-填充中的值不一致

C# 自动完成框-填充中的值不一致,c#,wpf,autocomplete,wpftoolkit,C#,Wpf,Autocomplete,Wpftoolkit,我有一个WPF应用程序,通过工具箱(VS2008)带有自动完成框。我有大约2000条记录的潜在人口,我尝试通过人口事件过程的组合来提高性能。我得到的结果不一致。该过滤器似乎是好的,但我可以运行应用程序一次,结果X将在那里,但结果Y不会。再次运行它可以使结果Y在那里而不是X,随后的时间X和Y都会在那里,等等。这是我第一次使用自动完成框,所以我确信它一定是我的代码中忘记的东西。如果我在Itemsource绑定之前检查我的结果集,那么所需的结果就在那里,但是用户看不到它们-下拉菜单autocomple

我有一个WPF应用程序,通过工具箱(VS2008)带有自动完成框。我有大约2000条记录的潜在人口,我尝试通过人口事件过程的组合来提高性能。我得到的结果不一致。该过滤器似乎是好的,但我可以运行应用程序一次,结果X将在那里,但结果Y不会。再次运行它可以使结果Y在那里而不是X,随后的时间X和Y都会在那里,等等。这是我第一次使用自动完成框,所以我确信它一定是我的代码中忘记的东西。如果我在Itemsource绑定之前检查我的结果集,那么所需的结果就在那里,但是用户看不到它们-下拉菜单autocomplete back不会显示。也许我需要一个事件覆盖

XAML


也许你可以看看这个。 所有为你做的工作


我不确定您首先为什么需要性能提升,您正在尝试计算另一个线程的自动完成框中应该包含的元素,然后将它们分配给控件的
ItemsSource
属性。AutoCompleteBox应该做类似的事情

我尝试将控件绑定到一个包含10000个字符串的列表,它工作得非常完美,因此您的问题可能是要放入集合中的对象的大小。一种解决方案是只使用字符串表示,然后当您需要所选对象时,您可以根据其表示找到它,假设它是唯一的(如果不是,您可以放置某种ID)

这种方法的一个主要问题是线程的sincronization,我现在将解释为什么会出现extrange行为,即使过滤器很好,结果中的项目也不正确

该过滤器似乎是好的,但我可以运行应用程序一次,结果X将 在那里,但你不会。再次运行它可以使结果为Y 有X而不是X,随后的时间X和Y都会在那里,等等, 等等

假设您在“自动完成”框中写入“ab”,这将启动一个新的
BackGroundWorker
,在那里执行此搜索。如果你等得够久,一切都会好的。但是,如果在第一个辅助进程完成之前更改搜索查询,那么现在所有结果都将混合。以以下代码行为例:

// the user searchs for "ab"
[Thread 1] Results.Clear();
[Thread 1] Results.Add(Item[1]);
[Thread 1] Results.Add(Item[2]);
...
// the user changes the search term (to "abc" for example)
[Thread 2] Results.Clear();
[Thread 2] Results.Add(Item[3]);
// but what would happen if the first BackGroundWorker hasn't finished yet,
// this means that the first thread is still running
[Thread 1] Results.Add(Item[5]); // this items doesn't match the second search
[Thread 1] Results.Add(Item[6]); // criteria, but are added to the collection
[Thread 2] Results.Add(Item[7]);
// then you'll have two treads adding items to the Result collection
[Thread 1] Results.Add(Item[2]);
...
[Dispatcher Thread] autGlobal.ItemsSource = Results;
[Dispatcher Thread] autGlobal.PopulateComplete();

希望这有帮助。

似乎这种情况正在起作用。。。“计数器>=结果的最大数量”。。我认为它几乎没有阻止任何记录。。
 private void AutoCompleteBox_Populating(object sender, PopulatingEventArgs e)
            {
            e.Cancel = true;
            var b = new BackgroundWorker();
            currSearch = autGlobal.Text;
            b.DoWork += b_DoWork;
            b.RunWorkerCompleted += b_RunWorkerCompleted;
            b.RunWorkerAsync(autGlobal.Text);
        }

private void b_DoWork(object sender, DoWorkEventArgs e)
        {
            Results.Clear();
            int counter = 0;
            string search = e.Argument.ToString();
            search = search.ToUpper();
            foreach (GlobalSearchList person in myGlobalList)
            {
                if (person.Item.ToUpper().Contains(search))
                {
                    Results.Add(person);
                    counter++;

                    if (counter >= MAX_NUM_OF_RESULTS)
                    {                        
                        break;
                    }
                }
            }
        }

private void b_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {

            if (this.Dispatcher.Thread == System.Threading.Thread.CurrentThread)
            {
                //Set the source
                if (currSearch == autGlobal.Text)
                {
                    autGlobal.ItemsSource = Results;
                    autGlobal.PopulateComplete();
                }
            }
            else
            {
                this.Dispatcher.Invoke(new Action(() =>
                {
                    //Set the source
                    if (currSearch == autGlobal.Text)
                    {
                        autGlobal.ItemsSource = Results;
                        autGlobal.PopulateComplete();
                    }

                }));
            }            
        }
// the user searchs for "ab"
[Thread 1] Results.Clear();
[Thread 1] Results.Add(Item[1]);
[Thread 1] Results.Add(Item[2]);
...
// the user changes the search term (to "abc" for example)
[Thread 2] Results.Clear();
[Thread 2] Results.Add(Item[3]);
// but what would happen if the first BackGroundWorker hasn't finished yet,
// this means that the first thread is still running
[Thread 1] Results.Add(Item[5]); // this items doesn't match the second search
[Thread 1] Results.Add(Item[6]); // criteria, but are added to the collection
[Thread 2] Results.Add(Item[7]);
// then you'll have two treads adding items to the Result collection
[Thread 1] Results.Add(Item[2]);
...
[Dispatcher Thread] autGlobal.ItemsSource = Results;
[Dispatcher Thread] autGlobal.PopulateComplete();