Skip to content

Instantly share code, notes, and snippets.

@jchannon
Created September 13, 2012 14:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jchannon/3714794 to your computer and use it in GitHub Desktop.
Save jchannon/3714794 to your computer and use it in GitHub Desktop.
Infragistics WPF Filtering
<Window x:Class="LanguageCategoryPhrase.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:igDP="http://infragistics.com/DataPresenter" xmlns:igWindows="http://infragistics.com/Windows"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="30*"/>
<ColumnDefinition Width="70*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition MaxHeight="30"></RowDefinition>
</Grid.RowDefinitions>
<ListBox Grid.Column="0" Grid.Row="0" Name="lbxCategories">
<ListBox.ItemTemplate>
<HierarchicalDataTemplate>
<CheckBox Content="{Binding Name}" IsChecked="{Binding IsChecked}"/>
</HierarchicalDataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<igDP:XamDataGrid Grid.Column="1" Grid.Row="0" Name="gridLanguage">
<igDP:XamDataGrid.FieldSettings>
<igDP:FieldSettings AllowEdit="False" />
</igDP:XamDataGrid.FieldSettings>
<igDP:XamDataGrid.FieldLayouts>
<igDP:FieldLayout IsDefault="False">
<igDP:FieldLayout.Settings>
<igDP:FieldLayoutSettings FilterAction="Hide" RecordFilterScope="AllRecords" RecordFiltersLogicalOperator="Or" RecordSelectorLocation="None" />
</igDP:FieldLayout.Settings>
</igDP:FieldLayout>
<igDP:FieldLayout IsDefault="False">
<igDP:Field Name="Title">
<igDP:Field.Settings>
<igDP:FieldSettings AllowEdit="True"></igDP:FieldSettings>
</igDP:Field.Settings>
</igDP:Field>
<igDP:Field Name="Visible" Visibility="Hidden"></igDP:Field>
<igDP:FieldLayout.Settings>
<igDP:FieldLayoutSettings FilterAction="Hide" RecordFilterScope="AllRecords" RecordFiltersLogicalOperator="Or" RecordSelectorLocation="None" />
</igDP:FieldLayout.Settings>
<igDP:FieldLayout.RecordFilters>
<igDP:RecordFilter FieldName="Visible">
<igWindows:ComparisonCondition Operator="Equals" Value="True"/>
</igDP:RecordFilter>
</igDP:FieldLayout.RecordFilters>
</igDP:FieldLayout>
<igDP:FieldLayout>
<igDP:Field Name="Name"></igDP:Field>
<igDP:Field Name="IsChecked" Visibility="Hidden"></igDP:Field>
<igDP:FieldLayout.Settings>
<igDP:FieldLayoutSettings FilterAction="Hide" RecordFilterScope="AllRecords" RecordFiltersLogicalOperator="Or" RecordSelectorLocation="None" />
</igDP:FieldLayout.Settings>
<igDP:FieldLayout.RecordFilters>
<igDP:RecordFilter FieldName="IsChecked">
</igDP:RecordFilter>
</igDP:FieldLayout.RecordFilters>
</igDP:FieldLayout>
</igDP:XamDataGrid.FieldLayouts>
</igDP:XamDataGrid>
<Button Grid.Row="1" Grid.ColumnSpan="2" Name="btnSave" Click="btnSave_Click" Content="Save"/>
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using Infragistics.Windows.Controls;
namespace LanguageCategoryPhrase
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public ObservableCollection<Category> Categories { get; set; }
public ObservableCollection<Language> Languages { get; set; }
public MainWindow()
{
InitializeComponent();
IEnumerable<Language> model = new[]
{
new Language
{
Culture="en-GB",
Title="United Kingdom",
Phrases = new[]
{
new Phrase
{
Title = "Start",
Visible = true,
Categories = new List<Category>
(
new[]
{
new Category() {Name = "KDV", IsChecked = true},
new Category() {Name = "GE", IsChecked = true}
}
)
},
new Phrase
{
Title = "Stop",
Visible = true,
Categories = new List<Category>
(
new[]
{
new Category() {Name = "KDV", IsChecked = true},
new Category() {Name = "ABC", IsChecked = true}
}
)
}
}
},
new Language
{
Culture="fr-FR",
Title="France",
Phrases = new[]
{
new Phrase
{
Title = "Start",
Visible = true,
Categories = new List<Category>
(
new[]
{
new Category() {Name = "KDV", IsChecked = true},
new Category() {Name = "GE", IsChecked = true}
}
)
},
new Phrase
{
Title = "Stop",
Visible = true,
Categories = new List<Category>
(
new[]
{
new Category() {Name = "KDV", IsChecked = true},
new Category() {Name = "ABC", IsChecked = true}
}
)
}
}
}
};
Categories =
new ObservableCollection<Category>(
model.SelectMany(x => x.Phrases.SelectMany(y => y.Categories)).GroupBy(x => x.Name).Select(
z => new Category(CategorySelected) { Name = z.Key, IsChecked = true }));
Languages = new ObservableCollection<Language>(model);
lbxCategories.ItemsSource = Categories;
gridLanguage.DataSource = Languages;
}
public void CategorySelected(object sender, PropertyChangedEventArgs e)
{
if (!this.IsVisible)
return;
var category = sender as Category;
if (category == null)
return;
foreach (var language in Languages)
{
foreach (var phrase in language.Phrases)
{
foreach (var phrasecategory in phrase.Categories)
{
if (category.Name == phrasecategory.Name)
{
phrasecategory.IsChecked = category.IsChecked;
}
}
phrase.Visible = !phrase.Categories.All(x => x.IsChecked == false);
}
}
gridLanguage.FieldLayouts[1].RecordFilters.Refresh();
gridLanguage.FieldLayouts[2].RecordFilters[0].Conditions.Clear();
if (category.IsChecked)
{
gridLanguage.FieldLayouts[2].RecordFilters[0].Conditions.Add(new ComparisonCondition(ComparisonOperator.Equals, true));
}
else
{
gridLanguage.FieldLayouts[2].RecordFilters[0].Conditions.Add(new ComparisonCondition(ComparisonOperator.NotEquals, false));
}
}
private void btnSave_Click(object sender, RoutedEventArgs e)
{
StringBuilder sb = new StringBuilder();
foreach (var language in Languages)
{
foreach (var phrase in language.Phrases)
{
sb.AppendLine(String.Format("Language : {0} - Phrase {1}", language.Title, phrase.Title));
}
}
MessageBox.Show(sb.ToString());
}
}
public class Language
{
public string Title { get; set; }
public string Culture { get; set; }
public IEnumerable<Phrase> Phrases { get; set; }
}
public class Phrase : INotifyPropertyChanged
{
private string title;
public event PropertyChangedEventHandler PropertyChanged;
public string Title
{
get { return title; }
set
{
title = value;
OnNotifychanged("Title");
}
}
public bool Visible { get; set; }
public List<Category> Categories { get; set; }
private void OnNotifychanged(string info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
}
public class Category : INotifyPropertyChanged
{
private readonly Action<object, PropertyChangedEventArgs> _callBack;
public event PropertyChangedEventHandler PropertyChanged;
private bool isChecked;
public Category()
{
_callBack = (o, args) => { };
}
public Category(Action<object, PropertyChangedEventArgs> callBack)
{
_callBack = callBack;
}
public string Name { get; set; }
public bool IsChecked
{
get { return isChecked; }
set
{
isChecked = value;
OnNotifychanged("IsChecked");
}
}
private void OnNotifychanged(string info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
_callBack(this, new PropertyChangedEventArgs(info));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment