I am trying to use a custom type in my filter where I want to show all distinct versions of my type in the list, and this works!
But when I select a value and try to filter on that value I get an error
filter has the value "{PTestType IsEqualTo ks k}" where "ks k" is one of my distint values but its like the PTestType is not converted using the typeconverter? Because then this is thrown
An exception of type 'System.ArgumentException' occurred in mscorlib.dll but was not handled in user code
Additional information: The value "(PTestType IsEqualTo ks k)" is not of type "Telerik.Windows.Data.IFilterDescriptor" and cannot be used in this generic collection.
Custom type Class
Type converter and valueconverter
My Custom filter codebehind
My usages of my custom filter
But when I select a value and try to filter on that value I get an error
var filter = new FilterDescriptor(dataMember, FilterOperator.IsEqualTo, checkBoxCustomFilterType.Text);
filter has the value "{PTestType IsEqualTo ks k}" where "ks k" is one of my distint values but its like the PTestType is not converted using the typeconverter? Because then this is thrown
An exception of type 'System.ArgumentException' occurred in mscorlib.dll but was not handled in user code
Additional information: The value "(PTestType IsEqualTo ks k)" is not of type "Telerik.Windows.Data.IFilterDescriptor" and cannot be used in this generic collection.
Custom type Class
namespace
Systematic.KVK.InseminationPlan.UIL.Details.Windows.TestBucket.DOM
{
[TypeConverter(
typeof
(TestTypeConverter))]
public
class
PTestType : IEquatable<PTestType>
{
public
long
Id {
get
;
set
; }
public
string
ShortTestName {
get
;
set
; }
public
string
TestName {
get
;
set
; }
public
bool
Equals(PTestType other)
{
return
Id == other.Id;
}
public
override
string
ToString()
{
return
ShortTestName;
}
public
override
int
GetHashCode()
{
return
(
int
)Id;
}
public
override
bool
Equals(
object
obj)
{
var type = obj
as
PTestType;
if
(type !=
null
)
{
return
Id == type.Id;
}
return
false
;
}
bool
IEquatable<PTestType>.Equals(PTestType other)
{
if
(other ==
null
)
{
return
false
;
}
return
StringComparer.Ordinal.Equals(Id, other.Id);
}
}
}
Type converter and valueconverter
namespace
Systematic.KVK.InseminationPlan.UIL.Details.Windows.TestBucket.Tabs.TestBucketTab
{
public
class
PTestTypeConverter : IValueConverter
{
public
object
Convert(
object
value, Type targetType,
object
parameter, CultureInfo culture)
{
var TestType = (PTestType)value;
return
TestType.ShortTestName;
}
public
object
ConvertBack(
object
value, Type targetType,
object
parameter, CultureInfo culture)
{
throw
new
NotImplementedException();
}
}
public
class
TestTypeConverter : TypeConverter
{
public
TestTypeConverter()
{
}
public
override
bool
CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
return
sourceType ==
typeof
(
string
);
}
public
override
object
ConvertFrom(ITypeDescriptorContext context, CultureInfo culture,
object
value)
{
var stringValue = value
as
string
;
if
(stringValue !=
null
)
{
return
new
PTestType {Id = 1, TestName =
"sdfsdf"
,ShortTestName =
"shortsd"
};
}
return
base
.ConvertFrom(context, culture, value);
}
public
override
bool
CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if
(destinationType ==
typeof
(
string
))
{
return
true
;
}
return
base
.CanConvertTo(context, destinationType);
}
public
override
object
ConvertTo(ITypeDescriptorContext context, CultureInfo culture,
object
value, Type destinationType)
{
if
(destinationType ==
typeof
(
string
))
{
return
((PTestType)value).ShortTestName;
}
return
base
.ConvertTo(context, culture, value, destinationType);
}
}
}
My Custom filter codebehind
namespace
Systematic.KVK.InseminationPlan.UIL.Details.Windows.TestBucket.CustomFilters
{
public
partial
class
TestTypeCheckBoxFilterControl : IFilteringControl
{
public
ObservableCollection<CheckBoxCustomFilterType> CustomItems {
get
;
set
; }
private
GridViewBoundColumnBase column;
private
CompositeFilterDescriptor compositeFilter;
public
TestTypeCheckBoxFilterControl()
{
InitializeComponent();
CustomItems =
new
ObservableCollection<CheckBoxCustomFilterType>();
DataContext =
this
;
}
private
void
OnFilter(
object
sender, RoutedEventArgs e)
{
column.DataControl.FilterDescriptors.Clear();
compositeFilter =
new
CompositeFilterDescriptor {LogicalOperator = FilterCompositionLogicalOperator.Or};
var dataMember = column.DataMemberBinding.Path.Path;
foreach
(var checkBoxCustomFilterType
in
CustomItems)
{
if
(checkBoxCustomFilterType.Checked)
{
var filter =
new
FilterDescriptor(dataMember, FilterOperator.IsEqualTo, checkBoxCustomFilterType.Text);
compositeFilter.FilterDescriptors.Add(filter);
}
}
if
(!column.DataControl.FilterDescriptors.Contains(compositeFilter))
{
column.DataControl.FilterDescriptors.Add(compositeFilter);
}
IsActive =
true
;
}
private
void
OnClear(
object
sender, RoutedEventArgs e)
{
column.DataControl.FilterDescriptors.Clear();
compositeFilter.FilterDescriptors.Clear();
CustomItems.ForEach(x => x.Checked =
false
);
}
public
void
Prepare(Telerik.Windows.Controls.GridViewColumn column)
{
CustomItems.Clear();
this
.column = column
as
GridViewBoundColumnBase;
var distinctValues = ((RadGridView)column.Parent).GetDistinctValues(column,
false
);
foreach
(PTestType distinctValue
in
distinctValues)
{
CustomItems.Add(
new
CheckBoxCustomFilterType {Checked =
false
, Text = distinctValue.ToString()});
}
}
public
bool
IsActive {
get
;
set
; }
private
void
SelectAll(
object
sender, RoutedEventArgs e)
{
var checkbox = (sender
as
CheckBox);
if
(checkbox ==
null
|| checkbox.IsChecked ==
null
)
{
return
;
}
foreach
(var checkBoxCustomFilterType
in
CustomItems)
{
checkBoxCustomFilterType.Checked = checkbox.IsChecked.Value;
}
}
}
}
My usages of my custom filter
<
telerik:GridViewDataColumn
DataMemberBinding
=
"{Binding Path=PTestType}"
IsReadOnly
=
"True"
IsFilterable
=
"True"
Header
=
"{x:Static localization:TestBucketTexts.TestType}"
>
<
telerik:GridViewDataColumn.FilteringControl
>
<
customFilters:TestTypeCheckBoxFilterControl
/>
</
telerik:GridViewDataColumn.FilteringControl
>
<
telerik:GridViewDataColumn.CellTemplate
>
<
DataTemplate
DataType
=
"dom:PBucketModel"
>
<
TextBlock
Text
=
"{Binding Path=PTestType}"
/>
</
DataTemplate
>
</
telerik:GridViewDataColumn.CellTemplate
>
</
telerik:GridViewDataColumn
>