Reputation: 478
I have a Value Converter running for an incoming string value from the database, to set the appropriate icon for a gender. The value coming in must be either M or F, and either one will display a male or female icon respectively. The Binding is working to some extent, in that the picture appears, but it is only displaying one set of icons for either value.
The value converter code is as follows:
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var gender = (string)value;
Uri uri;
uri = gender == "F" ?
new Uri("../Resources/Icons/female_user.png", UriKind.Relative) :
new Uri("../Resources/Icons/male_user.png", UriKind.Relative);
return uri;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
and the XAML is as follows
<Image Margin="8" Width="35" Height="35"
VerticalAlignment="Top" HorizontalAlignment="Center"
Source="{Binding Gender, Converter={StaticResource genderConverter}}" />
The resource is cited in the usercontrol.resources and all is properly bound I assume. So why does the converter persistently return only one value?
Upvotes: 0
Views: 596
Reputation: 478
All I needed was a gender.Trim() to get rid of all white spaces.
var gender = value.ToString();
try
{
return gender.Trim() == "F" ? "../Resources/Icons/male_user.png" : "../Resources/Icons/female_user.png";
}
catch (Exception)
{
return "";
}
Thats took care of it all, and it works. Thanks for the answers and "doh!" possibilites though.
Upvotes: 0
Reputation: 6339
Try this...
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
var gender = (string)value;
Uri uri;
uri = gender == "F" ?
new Uri("../Resources/Icons/female_user.png", UriKind.Relative) :
new Uri("../Resources/Icons/male_user.png", UriKind.Relative);
BitmapImage img_Gender = new BitmapImage(uri);
return img_Gender;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return null;
}
Upvotes: 0