我有一个值转换器为来自数据库的传入字符串值运行,为性别设置适当的图标。传入的值必须是 M 或 F,其中任何一个都将分别显示男性或女性图标。绑定在某种程度上是有效的,因为图片出现了,但它只为任一值显示一组图标。
值转换器代码如下:
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();
}
XAML如下
<Image Margin="8" Width="35" Height="35"
VerticalAlignment="Top" HorizontalAlignment="Center"
Source="{Binding Gender, Converter={StaticResource genderConverter}}" />
该资源在 usercontrol.resources 中被引用,并且我认为所有内容都已正确绑定。那么为什么转换器总是只返回一个值呢?