I have the following column to show a dropdown list to users with options "Yes", "No" or "N/A" and it is bound to a bool? property using a converter class that transforms the literals to true or false and for null values "N/A".
Here is the column definition:
<telerik:GridViewComboBoxColumn x:Name="BoolCol"
Width="165"
UniqueName="BoolCol"
DataMemberBinding="{Binding BoolCol, Mode=TwoWay, Converter={StaticResource BooleanToStringConverter}}"
DisplayMemberPath="Key"
SelectedValueMemberPath="Value"
Header="Bool Col">
</telerik:GridViewComboBoxColumn>
Here is the converter class:
public class BooleanToStringConverter : IValueConverter
{
static public string[] DefaultValues = new string[] { "Yes", "No", "N/A" };
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
var arr = (null != parameter) ? parameter.ToString().Split(',', ';', '|', '/')
: DefaultValues;
if (arr.Length != 3) { arr = DefaultValues; }
if (value is bool)
{
return (bool)value ? arr[0] : arr[1];
}
else
{
return arr[2];
}
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
var arr = (null != parameter) ? parameter.ToString().Split(',', ';', '|', '/')
: DefaultValues;
if (arr.Length != 3) { arr = DefaultValues; }
if (string.Equals((value ?? "").ToString(), arr[2], StringComparison.CurrentCultureIgnoreCase))
{
return null;
}
return string.Equals((value ?? "").ToString(), arr[0], StringComparison.CurrentCultureIgnoreCase);
}
}
The ItemsSource is set on the constructor like this:
var BoolCol = (DataGrid.Columns["BoolCol"] as GridViewComboBoxColumn);
BoolCol.DataType = typeof(System.Nullable<bool>);
BoolCol.ItemsSource = new Dictionary<string, string>() { { "Yes", "Yes"},
{ "No", "No" },
{ "N/A", "N/A"}};
This works fine except that when the user tries to filter by clicking on the column header... the resulting list shows 3 options as expected but instead of showing the values "Yes", "No" or "N/A" it only shows "N/A" an all 3 options.
It is even more puzzling that when you check on any of the options (which all read "N/A") it correctly filters by the right option.... The first one on the array is "Yes" so if you check for the first one it will filter the data for all the true values even if the dropdown on the filter reads "N/A".
Any advices??
Thanks