views:

91

answers:

3

Hi!

I have a CollectionView from an ObservableCollection:

private static ObservableCollection<CalculationViewModel> _calculations;

CalculationViewModelsCollection = (CollectionView)CollectionViewSource.GetDefaultView(_calculations);

My problem is that, when the result of the filter is nothing, I'd like to clear the filter, and refilter with other conditions, but the collectionView is always empty. It is only if the filter's result is nothing.

I tried to reset the filter these ways:

CalculationViewModelsCollection.Filter = null;
CalculationViewModelsCollection.Refresh();

and

CalculationViewModelsCollection.Filter = delegate(object p)
{
 return true;
};

None of them worked :( .

Could you give some advice how to reset a filter on a CollectionView?

Thanks in advance!

Regards, Zoli

+1  A: 

Hi there. From your example, I'm not entirely sure how you're getting your CollectionView, nor am I sure I understand your question correctly.

But anyway, I hope the sample code below helps you with your problem. It's an app that has a listbox containing strings, and a "filter" textbox. if nothing in the list matches the filter, the filter will be set to null and thus display all items.

XAML:

<Window x:Class="WpfApplication3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:v="clr-namespace:WpfApplication3"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel>
        <TextBox x:Name="textBox" TextChanged="TextBox_TextChanged"/>        
        <ListBox x:Name="listBox"/>
    </StackPanel>
</Window>

Code-behind:

public partial class MainWindow : Window
{
    ListCollectionView lcv;
    Predicate<object> filterFx;

    public MainWindow()
    {
        InitializeComponent();

        ObservableCollection<string> s = new ObservableCollection<string>();
        "The Quick Brown Fox Jumps Over The Lazy Dog"
            .Split(' ')
            .ToList()
            .ForEach((word) => s.Add(word.ToString()));

        this.lcv = new ListCollectionView(s);
        this.listBox.ItemsSource = this.lcv;

        this.filterFx = (p) => ((string)p).ToUpper().Contains(this.textBox.Text.ToUpper());
        lcv.Filter = this.filterFx;
    }

    private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
    {
        lcv.Refresh();

        if (lcv.Count == 0)
            lcv.Filter = null;
        else
            lcv.Filter = filterFx;
    }
}
karmicpuppet
A: 

Hi!

Thanks the example!

Yes, with your code it works perfectly. I don't undestand what is wrong in my program.

I try to find out what I'm doing wrong, because in my program, if the result of the filter is nothing, and set Filter to null to see all item in the collection, the collectionview is empty again.

If I find the mistake I will write it here! I hope it will be soonly :) !

Zoltán Barna
A: 

I did a big binding mistake. I don't understand how it works at all.

So the matter is that, it's simple to reset a filter, just set the value to null.

There is one more thing. I tried to create ListCollectionView like you did it.

this.lcv = new ListCollectionView(s);

But the filter didn't work, and I couldn't add SortDescription to the CollectionView.

I create CollectionView this way:

this.lcv = (CollectionView)CollectionViewSource.GetDefaultView(s);

and everything work fine. But ideally your technique have to work too.

Zoltán Barna
Hi. I see that you have replied. You should have used "Add Comment" instead of the "Add Another Answer" button so that I get notified.Anyway, I'm assuming that your ItemsSource is set to be equal to the variable "s" instead of "lcv", right? If you're going to use my approach, you'll have to set the ItemsSource equal to "lcv" instead. That should make it work.
karmicpuppet
Hi!Thanks for the advice, I'm just using stackoverflow two days ago and it is a bit strange for me now. :)Yeah, I have to set the ItemsSource equal to the CollectionView, not to the Collection.Thanks,Zoli
Zoltán Barna
That's ok. By the way, if you think I've answered your question on my original answer above, please accept my answer by clicking the check mark. Thanks.
karmicpuppet
I tried it before with the upper arrow, but it gave me that message:Vote Up requires 15 reputation. How can I accept your answer? I'm sorry but I can't find that check mark :( . Where is it?
Zoltán Barna
I found out what is the problem:I asked this question as an unregistered user, but after that, I registered to stackoverflow, and now I can't accept your answer, becauser there isn't a check mark below the arrows.I found this question: http://meta.stackoverflow.com/questions/1070/unregistered-user-how-can-i-return-to-my-question-to-select-an-answer-so-has-f.So I'm just write a mail for the stackoverflow team to merge my accounts, and I will able to accept your answer :) .
Zoltán Barna
Oh, thanks for doing that. =)
karmicpuppet
Your answer is accepted :) !
Zoltán Barna