Hi Team,
I am looking for some custom FilterCellValuePresenterStyle.? I am not able to bind my XamNumericEditor to values which force Field to filter.
Please suggest some good sample code so that by using them I could create my own Custom Filteration Template for my different Custom Objects.
What I did till now is,
I have object Money.cs : IComparable
{
public double Amount;
public double Currency;
public override string ToString() { return (!String.IsNullOrEmpty(currency) ? currency + " " + value : value); }
public int CompareTo(object obj)
if (Amount > ((Money)obj).Amount)
return 1;
if (Amount == ((Money)obj).Amount)
return 0;
return -1;
}
Xaml:- added XamNumericEditor
XAML.cs
if (field.DataType == typeof(Money))
field.Settings.FilterCellValuePresenterStyle = (Style)_grid.Resources["_moneyTypeFilterCellValuePresenter"];
What is not working here is filtration when ">=" for my value which is not in Combo dropdownlist.
Thanks
Nope. thanks alot for such good explanation.
i understand now..
HI,
Please let me know if you need further assistance regarding this issue.
Here is the developers comments:
Ok the first thing you tried where you just put a xamNumericEditor and bound its Value to the Value of the FilterCellValuePresenter will not work. The xamNumericEditor will have a Double for its Value. When one tries to do a comparison filter like >, >=, <, <=, =, etc. we try to take the value of the filter and convert that to the EditAsTypeResolved which in this case is your Money class. That fails because neither the double knows how to convert to Money nor does the Money know how to convert to double because neither has a TypeConverter that supports converting to/from the other format. That’s why you’ll see a number of first chance exceptions if you set up a sample the way they describe in their first post.
So the best thing to do is to define a TypeConverter for their Money class. At the very least they would want to support converting to/from string.
Note: This money class is based on your definition. I’m really only including it below to show how one defines a typeconverter for a type – namely by putting a TypeConverter attribute on the class that indicates the type of the TypeConverter. Personally I would do it differently using decimal for the amount, making the Amount/Currency immutable since no one would know about changes to the value, probably make it a structure instead of a class, etc.Also note that the TypeConverter is a simplistic implementation – you will likely want to adjust the conversion to take the currency, handle double parsing better, etc.
[TypeConverter(typeof(MoneyConverter))]
public class Money : IComparable
public string Currency;
public override string ToString()
return (!String.IsNullOrEmpty(Currency) ? Currency + " " + Amount : Amount.ToString());
public class MoneyConverter : TypeConverter
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
if (sourceType == typeof(string))
return true;
return base.CanConvertFrom(context, sourceType);
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
if (destinationType == typeof(string))
return base.CanConvertTo(context, destinationType);
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
if (value is string)
var d = double.Parse(value as string, culture);
return new Money { Amount = d };
return base.ConvertFrom(context, culture, value);
publicoverride object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
if (value is Money)
var m = value as Money;
return m.Amount.ToString(culture);
return base.ConvertTo(context, culture, value, destinationType);
If you did nothing else (i.e. they just had a datasource that had a list of items with properties of type Money) everything would just work. The default filter combo would still be present with the list of existing values and custom options like blanks, custom, etc. AND you could type in numeric values and use the comparison operators (e.g. >=, etc.). I would recommend this approach as you get all the additional functionality and they don’t need to retemplate the FilterCellValuePresenter, etc.
If you did do what I describe above but you still tried to use the custom FilterCellValuePresenter template that had a xamNumericEditor in it (like you did in the first forum post) you would find that you would need to adjust the TypeConverter such that it also supports converting to/from double.
The alternative to all of this would be something like you tried in the last response on the forum post which is to define a IValueConverter and use that on the Value binding in the xamNumericEditor. That sort of works because then the filter value is a Money and so no conversion is necessary. To me it’s less than ideal since you need to re-template the element which means losing theming, etc. but also because you lose the options to get to the custom filter dialog and because the comparison operator list still includes other options that don’t make sense like matching regular expressions so they’d need to set things like the FilterOperatorDropDownItems on the FieldSettings to limit the list.
With regards to your comment “But if I use this then the value of Filter changes to USD 5.00 from 5.00 using convertback method” in the last forum post, I’m guessing this relates to the fact that you tried in your IValueConverter to store/cache the last Currency that came through the Convert method but that doesn’t seem like a good idea. I mean if even if the converter weren’t shared amongst multiple instances (which it may well be depending on where/how it is defined) I don’t think you can assume when/if the methods will be called or the order and also you have to consider that there may not have been a filter value so there would have been no Money instance to convert and therefore nothing stored. But that’s really up to you to figure out.
Hi Matt, Thanks for quick reply.
Apologies for half information in last mail.
So after selecting >= please type 5.00 in filtering . it through exception that it cannot convert it into Money. So then i attached converter as well. like below
public class MoneyToDoubleConverter : IValueConverter { public MoneyToDoubleConverter() { _currency = string.Empty; }
private string _currency;
public bool CanConvert(Type sourceType, Type targetType) { return true; }
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (value is Money) { _currency = (value as Money).Currency; } if (value != null && Object.Equals(value.GetType(), typeof(General.Money))) { return ((Money)value).Amount; } return value; }
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { return new Money(value is double ? (double)value : 0d, _currency); }
But if I use this then the value of Filter changes to USD 5.00 from 5.00 using convertback method.
*********************
==> What i would want here is to allow user to type only 5.00 andfilter the columns.thats why I wrote FilterCellvaluePresenter Style(sent you in first mail) and replaced XamComboEditor to XamNumericEditor.
Please read my FIRST mail and help me know why is this binding of XamNumericEditor not forcing it to filter.
<Editors:XamNumericEditor Grid.Column="1" Value="{Binding Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}, Path=Value, Converter={StaticResource _moneyToDoubleConverter}UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
AND most importantly if available, please send me some sample code of FiltercellValuePresent style and FilterCellEditor styling.
thanks
I created a sample with your code and filtering is now working.
Please review the attached sample.