Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
};
like image 661
Steve Avatar asked Oct 25 '22 18:10

Steve


2 Answers

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.

like image 73
jellyfish Avatar answered Oct 27 '22 10:10

jellyfish


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.

like image 25
Steve Avatar answered Oct 27 '22 11:10

Steve