Hello,
This behavior is expected. There is no automatic way to translate the string that the user types in into your custom type.
One thing you can try to do is to specify a TypeConverter for your custom types, so that conversion to and from string would be possible.
Another thing that you can do is create a custom filter editor and assign it to be used by the respective column. There is a virtual method called CreateFieldFilterEditor. You can override it and supply your custom editor. Your custom editor should "know" how to produce instances of your custom type. Here is an example of something similar -- the idea would be exactly the same:
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using Telerik.Windows.Controls;
using Telerik.Windows.Data;
namespace Testing
{
public class TimeSpanColumn : GridViewBoundColumnBase
{
public override FrameworkElement CreateFieldFilterEditor()
{
var editor = new TextBox();
// "Value" is the name of the property on our underlying ViewModel.
// You have to bind your editor to this property.
var textBinding = new Binding("Value")
{
Mode = BindingMode.TwoWay,
FallbackValue = string.Empty,
Converter = new TimeSpanConverter()
};
editor.SetBinding(TextBox.TextProperty, textBinding);
// Remove these if you don't like them
TextBoxBehavior.SetUpdateTextOnEnter(editor, true);
TextBoxBehavior.SetSelectAllOnGotFocus(editor, true);
return editor;
}
private class TimeSpanConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
// FilterDescriptor.UnsetValue means "the filter is not active"
if (value == FilterDescriptor.UnsetValue)
{
return string.Empty;
}
return value.ToString();
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (!string.IsNullOrEmpty((string)value))
{
var result = TimeSpan.Zero;
if (TimeSpan.TryParse((string)value, out result))
{
return result;
}
}
// FilterDescriptor.UnsetValue means "deactivate the filter"
return FilterDescriptor.UnsetValue;
}
}
}
}
The idea is that the custom editor will take the string that the user types in, convert it to the custom Type (TimeSpan in my sample) and then assign it to the
Value property of our view model.
I hope this helps. Let me know if there are problems.
All the best,
Ross
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license!
Book your seat now >>