自动完成框 - 总体值不一致

发布于 2024-12-23 02:10:09 字数 2603 浏览 0 评论 0原文

我有一个通过工具包(VS 2008)带有自动完成框的WPF应用程序。我有大约 2000 条记录的潜在填充,并且我尝试结合填充事件过程来提高性能。我得到的结果不一致。过滤器似乎没问题,但我可以运行应用程序一次,结果 X 会出现,但结果 Y 不会。再次运行它可以使结果 Y 出现而不是 X,随后 X 和 Y 都会出现,等等。这是我第一次使用自动完成框,所以我确信它一定是我的代码中的某些内容我忘了。如果我在 Itemsource 绑定之前检查结果集,则所需的结果就在那里,但用户看不到它们 - 下拉自动完成后面不会显示。也许我需要一个事件覆盖???

XAML

<input:AutoCompleteBox                         
Name="autGlobal"
FilterMode="Contains"
Style="{DynamicResource MiniSearchAutoBoxWPF}"
IsTextCompletionEnabled="false" 
Margin="5, 0, 5, 0" 
HorizontalAlignment="Center"
KeyUp="autGlobal_KeyUp"
Text="Search Term" 
GotFocus="autGlobal_GotFocus"
ValueMemberPath="Item" 
Populating="AutoCompleteBox_Populating"
>

方法

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

                }));
            }            
        }

I have a WPF application with the autocomplete box via the toolkit (VS 2008). I have a potential population of about 2000 records and I have tried to improve performance with a combination of the populating event procedure. I am getting inconsistent results. The filter seems to be OK but I can run the app once and result X will be there but result Y wont. Running it again can make result Y be there and not X, subsequent times both X and Y will be there, etc, etc. This is my first time using the autocomplete box so I'm sure it must be something in my code that I'm forgetting. If I check my result set just prior to the Itemsource binding, the desired results are there, but they are not made visible to the user - the drop-down autocomplete back does not show. Maybe I need an event override???

The XAML

<input:AutoCompleteBox                         
Name="autGlobal"
FilterMode="Contains"
Style="{DynamicResource MiniSearchAutoBoxWPF}"
IsTextCompletionEnabled="false" 
Margin="5, 0, 5, 0" 
HorizontalAlignment="Center"
KeyUp="autGlobal_KeyUp"
Text="Search Term" 
GotFocus="autGlobal_GotFocus"
ValueMemberPath="Item" 
Populating="AutoCompleteBox_Populating"
>

The Methods

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

                }));
            }            
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情徒 2024-12-30 02:10:09

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

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

这种方法的主要问题之一是线程同步化,我现在将解释为什么你会得到奇怪的行为,即使过滤器很好,结果中的项目也不正确。

过滤器似乎没问题,但我可以运行该应用程序一次,结果 X 将
存在,但结果 Y 不会。再次运行可以得到结果Y为
有而不是 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();

希望这会有所帮助。

I'm not sure why you need the performance boost in the first place, you're trying to calculate the elements that should be in the Autocomplete box in another thread and then assign them to the ItemsSource property of the Control. Something similar is what the AutoCompleteBox should do.

I tryed bind the control to a list with 10000 strings and it works perfect, so your problem could be the size of the objects that you're putting in the collection. One solution could be use just a string representation and then when you need the selected object you could find it based on it's representation, assuming that is unique (if not you could put some sort of ID).

One of the main problems with this approach is the thread sincronization, i will explain now why you get the extrange behavior where even when the filter is fine the items in the results are not right.

The filter seems to be OK but I can run the app once and result X will
be there but result Y wont. Running it again can make result Y be
there and not X, subsequent times both X and Y will be there, etc,
etc.

Suppose that you write "ab" in the autocomplete box, this will start a new BackGroundWorker where this search is performed. Everything should be fine if you wait long enough. But if you change the search query before the first worker has finished, now all the results will be mixed. Take for example the following lines of code:

// 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();

Hope this helps.

一梦浮鱼 2024-12-30 02:10:09

也许你可以检查一下这个。
所有工作都为您完成。

http://gallery.expression.microsoft.com/WPFAutoCompleteBox/

Maybe you could check this one.
All the work done for you.

http://gallery.expression.microsoft.com/WPFAutoCompleteBox/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文