Giter Club home page Giter Club logo

Comments (4)

abid76 avatar abid76 commented on August 20, 2024 2

@mdomas1
MyListView.xaml:

<UserControl x:Class="MyListView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:dgx="urn:tom-englert.de/DataGridExtensions">
   <DockPanel>
        <TextBox DockPanel.Dock="Top" Text="{Binding GlobalFilterText, UpdateSourceTrigger=PropertyChanged}" />
        <DataGrid 
                  ItemsSource="{Binding ItemListView}" dgx:DataGridFilter.GlobalFilter="{Binding GlobalFilter}">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Title" Binding="{Binding Title}" />
                <DataGridTextColumn Header="User"  Binding="{Binding Username}" />
                <!-- ... -->
            </DataGrid.Columns>
        </DataGrid>
</UserControl>

MyListViewModel.cs:

public class MyListViewModel : ViewModelBase
{
    public ListCollectionView ItemListView { get; set; } // Collection of MyListItem items

    private string globalFilterText;
    public string GlobalFilterText
    {
        get => globalFilterText;
        set 
        {  
            if (Set(ref globalFilterText, value))
            {
                GlobalFilter = null;
                GlobalFilter = ApplyGlobalFilter;
            }
        }
    }

    protected override bool ApplyGlobalFilter(object obj)
    {
            var text = GlobalFilterText?.Trim();
            if (string.IsNullOrEmpty(text))
                return true;

            var terms = Regex.Split(text, @"\s+");

            var item = obj as MyListItem;
            return
                terms.All(term =>
                {
                    // filter on displayed properties (columns) of MyListItem
                    item.Title.Contains(term, StringComparison.OrdinalIgnoreCase) ||
                    item.Username.Contais(term, StringComparison.OrdinalIgnoreCase);
                });
    }
}

ViewModelBase.cs

public abstract class ViewModelBase : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        protected bool Set<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
        {
            if (Equals(storage, value)) return false;

            storage = value;
            OnPropertyChanged(propertyName);
            return true;
        }

        private bool Equals<TKey, TValue>(Dictionary<TKey, TValue> dict1, Dictionary<TKey, TValue> dict2)
        {
            bool equal = false;
            if (dict1.Count == dict2.Count)
            {
                equal = true;
                foreach (var pair in dict1)
                {
                    TValue value;
                    if (dict2.TryGetValue(pair.Key, out value))
                    {
                        // Require value be equal.
                        if (!Equals(pair.Value, value))
                        {
                            equal = false;
                            break;
                        }
                    }
                    else
                    {
                        // Require key be present.
                        equal = false;
                        break;
                    }
                }
            }
            return equal;
        }
}

from datagridextensions.

abid76 avatar abid76 commented on August 20, 2024

I finally found it out. Problem was, that firing PropertyChangedEvent on GlobalFilter had no effect.

I solved it by setting GlobalFilter to null and then back to the predicate.

View:

<TextBox Text="{Binding GlobalFilterText, UpdateSourceTrigger=PropertyChanged}" />
<DataGrid dgx:DataGridFilter.GlobalFilter="{Binding GlobalFilter}">
  <DataGrid.Columns>
    <!-- .... -->
  </DataGrid.Columns>
</DataGrid>

ViewModel:

private string globalFilterText;
public string GlobalFilterText
{
    get => globalFilterText;
    set 
    { 
        if (Set(ref globalFilterText, value))
        {
            // OnPropertyChanged(() => GlobalFilter); commented out as it has no effect.
            GlobalFilter = null;
            GlobalFilter = ApplyGlobalFilter;
        }
    }
}

protected virtual bool ApplyGlobalFilter(object obj)
{
    // apply the filtering
}

from datagridextensions.

tom-englert avatar tom-englert commented on August 20, 2024

😃

from datagridextensions.

mdomas1 avatar mdomas1 commented on August 20, 2024

Hello @abid76 ,
Would you mind sharing your full C# code for getting the Global Filter to work? I am struggling as well. I just want to be able to have a textbox with a keyword that filters the rows of my datagrid. Unfortunately, I cannot use the AutoFilter as the way I databind isn't compatible, but I believe if I do a custom Global Filter it should work.

from datagridextensions.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.