简体   繁体   中英

SL4 AutoCompleteBox repeating filter results issue

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM