SL4 AutoCompleteBox repeating filter results issue

别来无恙 提交于 2019-12-07 04:45:16

问题


I'm having an issue with the AutoCompleteBox filtering.

It seems to be rembering the previous filter.

For example I type in 'A' and it returns 1 item. I delete the 'A' and type in 'Z' which should return 1 item.

The problem is it returns the results from the 'A' filter plus the 'Z', I delete 'Z' and type 'S' which brings back 2 items and it now displays the results from all 3 filters.

Am I doing something wrong?

stockTypes.Add(new StockTypeDTO() { Description = "Steel Coil", StockCode = "SC" });
stockTypes.Add(new StockTypeDTO() { Description = "Palletised Steel Coil", StockCode = "PS" });
stockTypes.Add(new StockTypeDTO() { Description = "ZZZZZ", StockCode = "ZZ" });


<input:AutoCompleteBox x:Name="testauto" FilterMode="Custom">
    <input:AutoCompleteBox.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <ContentPresenter Content="{Binding Description}" />
            </StackPanel>
        </DataTemplate>
    </input:AutoCompleteBox.ItemTemplate>
</input:AutoCompleteBox>

testauto.ItemsSource = this.StockTypes;

testauto.ItemFilter = (search, item) =>
{
    StockTypeDTO stockType = item as StockTypeDTO;

    if (stockType != null)
    {
        string filter = search.ToUpper(CultureInfo.InvariantCulture);
        return (stockType.StockCode.ToUpper(CultureInfo.InvariantCulture).Contains(filter)
        || stockType.Description.ToUpper(CultureInfo.InvariantCulture).Contains(filter));
    }

    return false;
};

回答1:


Also, the previous results are shown, but treated like they are non-existent right? I mean, selecting them don't change the autocompletebox's value? I'm having the same issue, got it after changing the style. In my situation it's because of ListBox's style. If you're using a custom style for listbox, try removing it & use the default style.




回答2:


I ended up inheriting AutoCompleteBox capturing the Populated event and performing this hack.

var listBox = this.GetTemplateChild("Selector") as ListBox; 
var items = listBox.ItemsSource; 
listBox.ItemsSource = null; 
listBox.ItemsSource = items;

It fixed the problem, I'm sure there's a cleaner way of doing it but I didn't have time to mess around with it.



来源:https://stackoverflow.com/questions/5141529/sl4-autocompletebox-repeating-filter-results-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!