Ok, here's the deal. I have a CollectionViewSource:
<CollectionViewSource x:Key="PA_System_AppStatus">
<CollectionViewSource.Source>
<SystemCols:ArrayList>
<ComboBoxItem Content="Active" />
<ComboBoxItem Content="Denied" />
<ComboBoxItem Content="Granted" />
</SystemCols:ArrayList>
</CollectionViewSource.Source>
</CollectionViewSource>
I also have a ComboBox bound to that:
<ComboBox x:Name="Perro" Tag="Application"
SelectedValue="{Binding Path=[AppStatus], Converter={StaticResource AppStatusConverter}}"
ItemsSource="{Binding Source={StaticResource PA_System_AppStatus2}}"/>
AppStatus is a char in a DataRow that may be A,D,G. Therefore, I want the ComboBox to display the whole ComboBoxItem above, but under the hood store in the field the char. For that, I wrote this Converter:
public class AppStatusConverter : IValueConverter
{
public object Convert(
object value,
System.Type targetType,
object parameter,
System.Globalization.CultureInfo culture
)
{
string returnValue = null;
if (value != System.DBNull.Value && value != null)
{
if ((string)value == "A")
returnValue = "Active";
else if ((string)value == "D")
returnValue = "Denied";
else if ((string)value == "G")
returnValue = "Granted";
else
returnValue = null;
}
return returnValue;
}
public object ConvertBack(
object value,
System.Type targetType,
object parameter,
System.Globalization.CultureInfo culture
)
{
string returnValue = null;
string tempvalue = ((ComboBoxItem)value).Content.ToString();
if (tempvalue == "Active")
returnValue = "A";
else if (tempvalue == "Denied")
returnValue = "D";
else if (tempvalue == "Granted")
returnValue = "G";
else
returnValue = null;
return returnValue;
}
}
The ConvertBack part works perfectly and, whenever I choose a value, the DataRow gets filled with one of the chars (A,D or G).
However, the Convert does not. For instance, I load a DataRow from a DB. The Converter then correctly takes the value inside the 'AppStatus' column and tries to convert it to select one of the ComboBox Items and assign the SelectedValue. However, nothing happens.