Silverlight 值转换器
我有一个值转换器,用于从数据库传入的字符串值,为性别设置适当的图标。传入的值必须是 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 中引用,并且我假设所有资源都已正确绑定。那么为什么转换器始终只返回一个值呢?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个...
Try this...
我所需要的只是一个 sex.Trim() 来消除所有空格。
这一切都解决了,而且有效。感谢您的回答和“doh!”不过有可能。
All I needed was a gender.Trim() to get rid of all white spaces.
Thats took care of it all, and it works. Thanks for the answers and "doh!" possibilites though.