Comments (4)
@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.
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.
😃
from datagridextensions.
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)
- drag values
- Filtering is not working with converters HOT 7
- IsAutoFilterEnabled is not supported when ItemsSource is a DataTable type HOT 20
- Feature Request: Nested Columns HOT 1
- FileNotFoundException HOT 1
- Migrate signing step from signing server to CLI HOT 2
- IsAutoFilterEnabled Binding not working HOT 4
- Is it possible to implement tree-shaped DataGridExtensions? HOT 1
- MultipleChoice filter HOT 3
- GetFilter().Clear doesn't clear the Listbox of the MultiplechoiceFilters
- How to use HOT 1
- How can i get filter result data HOT 2
- Column width gets bigger HOT 3
- Using AutoFilters and own Filtering HOT 1
- [1.0.41] IsFilterVisible doesn't work for single column when IsAutoFilterEnable set to False HOT 1
- Using DataGridExtensions from code HOT 2
- Part URI cannot start with two forward slashes. HOT 2
- MultipleChoiceFilter also working with DataGridTemplateColumn? HOT 1
- Caliburn/ninject and Binding Filter HOT 1
- DataGridFilterHost does not have AllFilterChanged event HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from datagridextensions.