macgile / DataGridFilter

WPF Filterable Datagrid, multi language
MIT License
371 stars 77 forks source link

Output of filtered data #173

Open hanyscz opened 3 weeks ago

hanyscz commented 3 weeks ago

Hi, how can I enable getting filtered data into another collection? Is there an output, or is it not possible? Maybe I’m looking at it incorrectly.

Basnederveen commented 3 weeks ago

Have the same question, somehow I would like to bind to the filtered collection in my viewmodel

macgile commented 3 weeks ago

Hi, You can see demo app

Basnederveen commented 3 weeks ago

I see in the sample app you are binding the 'FilteredList', but the filteredlist is computed in the ViewModel.

I would like the user to use the filter menu's and iterate over what is left in the UI from a command.

Am I missing something?

macgile commented 3 weeks ago

be more explicit, I don't understand what you want to do, make pseudo code if necessary

Basnederveen commented 3 weeks ago

Thanks for the reply. I see i wasn't very clear in my previous comment ;)

What I mean is, if I have 8 items in my ObservableCollection that is bound to the DataGrid, and I filter it down to 2 items by using the popup menu's, how do I access the filtered collection in my ViewModel.

Filtering the DataGrid does not change the source, and that's all I have in the ViewModel.

I saw you wrote the .Items property contains the filtered collection, but in that case I can't find a 'clean' way to get this collection into my VM.

EDIT: Only way I can do it now is by doing this in the code-behind:

public partial class PartsGridPage : Page
{
    public PartsGridPage(PartsGridViewModel viewModel)
    {
        InitializeComponent();
        DataContext = viewModel;

        PartsGrid.Items.CurrentChanged  += Items_CurrentChanged;
    }

    private void Items_CurrentChanged(object? sender, EventArgs e)
    {
        ((PartsGridViewModel)DataContext).FilteredSource = PartsGrid.Items.Cast<PartGridItemViewModel>().ToList();
    }
}
macgile commented 3 weeks ago

Your solution seems correct to me. I agree with you that having to cast the Items collection to get the resulting collection from the filtering is not elegant at all, but I haven't found any other way to do it. If you look at the source code of FilterDataGrid, I call this method several times.

macgile commented 3 weeks ago

I worked for several days to find an alternative and finally I had to resign myself because the code I produced was not more efficient. if you find an alternative, let me know