In order to be able to assign null
to the Published
property, you would have to change its type to Nullable< bool > (you can write bool?
in C#).
public bool? Published
{
...
}
The Converter could be implemented so that it converts from string
to bool
and vice versa, perhaps like shown below. Note that the converter uses bool
, not bool?
since the value is passed to and from the converter as object
, and hence boxed anyway.
public class YesNoAllConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
object result = "All";
if (value is bool)
{
result = (bool)value ? "Yes" : "No";
}
return result;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
object result = null;
switch ((string)value)
{
case "Yes":
result = true;
break;
case "No":
result = false;
break;
}
return result;
}
}
To enable the use of this converter, you have to change your ComboBox item type to string
, and bind to the SelectedItem property, not SelectedValue.
<ComboBox SelectedItem="{Binding Path=Published, Mode=TwoWay,
Converter={StaticResource YesNoAllConverter}}">
<sys:String>All</sys:String>
<sys:String>Yes</sys:String>
<sys:String>No</sys:String>
</ComboBox>
where sys
is the following xml namespace declaration:
xmlns:sys="clr-namespace:System;assembly=mscorlib"