Silverlight 值转换器

发布于 2024-11-17 14:03:09 字数 922 浏览 7 评论 0原文

我有一个值转换器,用于从数据库传入的字符串值,为性别设置适当的图标。传入的值必须是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尛丟丟 2024-11-24 14:03:09

试试这个...

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;
        }

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;
        }
一曲爱恨情仇 2024-11-24 14:03:09

我所需要的只是一个 sex.Trim() 来消除所有空格。

var gender = value.ToString();
        try
        {
            return gender.Trim() == "F" ? "../Resources/Icons/male_user.png" : "../Resources/Icons/female_user.png";
        }
        catch (Exception)
        {
            return "";
        }

这一切都解决了,而且有效。感谢您的回答和“doh!”不过有可能。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文