001.
public
partial
class
CheckBoxFilterControl : IFilteringControl
002.
{
003.
public
ObservableCollection<CheckBoxCustomFilterType> CustomItems {
get
;
set
; }
004.
private
GridViewBoundColumnBase column;
005.
private
CompositeFilterDescriptor compositeFilter;
006.
007.
public
CheckBoxFilterControl()
008.
{
009.
InitializeComponent();
010.
CustomItems =
new
ObservableCollection<CheckBoxCustomFilterType>();
011.
BlankText =
"(Blank)"
;
012.
DataContext =
this
;
013.
014.
Loaded += Initialize;
015.
}
016.
017.
private
void
Initialize(
object
sender, RoutedEventArgs e)
018.
{
019.
Loaded -= Initialize;
020.
021.
PopUpParent.Opened += (s, a) => SetCachedValues();
022.
PopUpParent.Closed += (s, a) => CheckCachedValues();
023.
}
024.
025.
private
void
CheckCachedValues()
026.
{
027.
if
(cachedCustomItems ==
null
)
return
;
028.
029.
foreach
(var item
in
CustomItems)
030.
{
031.
var cachedItem = cachedCustomItems.Single(ci => ci.InternalValue == item.InternalValue && ci.Text == item.Text);
032.
if
(item.Checked != cachedItem.Checked)
033.
{
034.
item.Checked = cachedItem.Checked;
035.
}
036.
}
037.
038.
if
(SelectAllCheckBox.IsChecked.GetValueOrDefault() != cachedIsAllSelected)
039.
{
040.
SelectAllCheckBox.IsChecked = cachedIsAllSelected;
041.
}
042.
}
043.
044.
private
List<CheckBoxCustomFilterType> cachedCustomItems;
045.
private
bool
cachedIsAllSelected;
046.
private
void
SetCachedValues()
047.
{
048.
cachedIsAllSelected = SelectAllCheckBox.IsChecked.GetValueOrDefault();
049.
cachedCustomItems = CustomItems.Select(i=>
new
CheckBoxCustomFilterType(){Checked = i.Checked, InternalValue = i.InternalValue, Text = i.Text}).ToList();
050.
}
051.
052.
private
Popup internalPopUpParent =
null
;
053.
private
Popup PopUpParent
054.
{
055.
get
{
return
internalPopUpParent ?? (internalPopUpParent =
this
.ParentOfType<Popup>()); }
056.
}
057.
058.
private
void
OnFilter(
object
sender, RoutedEventArgs e)
059.
{
060.
if
(compositeFilter !=
null
)
061.
{
062.
column.DataControl.FilterDescriptors.Remove(compositeFilter);
063.
}
064.
065.
compositeFilter =
new
CompositeFilterDescriptor {LogicalOperator = FilterCompositionLogicalOperator.Or};
066.
var dataMember = column.DataMemberBinding.Path.Path;
067.
foreach
(var checkBoxCustomFilterType
in
CustomItems)
068.
{
069.
if
(checkBoxCustomFilterType.Checked)
070.
{
071.
var filter =
new
FilterDescriptor(dataMember, FilterOperator.IsEqualTo, checkBoxCustomFilterType.InternalValue);
072.
compositeFilter.FilterDescriptors.Add(filter);
073.
}
074.
}
075.
076.
if
(!column.DataControl.FilterDescriptors.Contains(compositeFilter))
077.
{
078.
column.DataControl.FilterDescriptors.Add(compositeFilter);
079.
}
080.
081.
SetCachedValues();
082.
083.
IsActive =
true
;
084.
085.
PopUpParent.IsOpen =
false
;
086.
}
087.
088.
private
void
OnClear(
object
sender, RoutedEventArgs e)
089.
{
090.
if
(compositeFilter !=
null
)
091.
{
092.
column.DataControl.FilterDescriptors.Remove(compositeFilter);
093.
}
094.
095.
CustomItems.ForEach(x => x.Checked =
false
);
096.
097.
SetCachedValues();
098.
099.
IsActive =
false
;
100.
101.
PopUpParent.IsOpen =
false
;
102.
}
103.
104.
public
void
Prepare(GridViewColumn c)
105.
{
106.
column = c
as
GridViewBoundColumnBase;
107.
108.
if
(column ==
null
)
109.
{
110.
return
;
111.
}
112.
113.
var distinctValues = ((RadGridView) column.Parent).GetDistinctValues(column,
false
);
114.
115.
foreach
(var distinctValue
in
distinctValues)
116.
{
117.
if
(CustomItems.Any(x => x.Text == DistinctValueToString(distinctValue)))
118.
{
119.
continue
;
120.
}
121.
122.
CustomItems.Add(
new
CheckBoxCustomFilterType
123.
{
124.
Checked =
false
,
125.
Text = DistinctValueToString(distinctValue),
126.
InternalValue = distinctValue
127.
});
128.
}
129.
if
(column !=
null
)
130.
{
131.
RegisterItemsSourceChanged(column.DataControl,
true
);
132.
}
133.
}
134.
135.
private
void
RegisterItemsSourceChanged(GridViewDataControl dataControl,
bool
register)
136.
{
137.
var property = DependencyPropertyDescriptor.FromProperty(DataControl.ItemsSourceProperty,
typeof
(GridViewDataControl));
138.
if
(property !=
null
)
139.
{
140.
if
(register)
141.
{
142.
property.AddValueChanged(dataControl, RemoveFilterWhenItemsSourceIsChanged);
143.
}
144.
else
145.
{
146.
property.RemoveValueChanged(dataControl, RemoveFilterWhenItemsSourceIsChanged);
147.
}
148.
}
149.
}
150.
151.
private
void
RemoveFilterWhenItemsSourceIsChanged(
object
sender, EventArgs e)
152.
{
153.
if
(compositeFilter !=
null
)
154.
{
155.
column.DataControl.FilterDescriptors.Remove(compositeFilter);
156.
}
157.
158.
RegisterItemsSourceChanged(column.DataControl,
false
);
159.
}
160.
161.
private
string
DistinctValueToString(
object
distinctValue)
162.
{
163.
return
distinctValue ==
null
? BlankText : distinctValue.ToString();
164.
}
165.
166.
public
bool
IsActive
167.
{
168.
get
{
return
(
bool
)GetValue(IsActiveProperty); }
169.
set
{ SetValue(IsActiveProperty, value); }
170.
}
171.
172.
public
string
BlankText {
get
;
set
; }
173.
174.
public
static
readonly
DependencyProperty IsActiveProperty =
175.
DependencyProperty.Register(
176.
"IsActive"
,
177.
typeof
(
bool
),
178.
typeof
(CheckBoxFilterControl),
179.
new
PropertyMetadata(
false
));
180.
181.
private
void
SelectAll(
object
sender, RoutedEventArgs e)
182.
{
183.
var checkbox = (sender
as
CheckBox);
184.
if
(checkbox ==
null
|| checkbox.IsChecked ==
null
)
185.
{
186.
return
;
187.
}
188.
189.
foreach
(var checkBoxCustomFilterType
in
CustomItems)
190.
{
191.
checkBoxCustomFilterType.Checked = checkbox.IsChecked.Value;
192.
}
193.
}
194.
}