0

我有一个绑定到数据网格的匿名 linq 查询,当我调试它时,它会带来正确的数据,但它没有显示在数据网格中,我怀疑在将它绑定到数据网格之前,对 RIA 服务的请求没有完成. 我可以使用 LoadOperation<>() Completed 事件。但它只适用于定义的实体,所以我该怎么做呢?参考这里是最后一篇文章: LINQ 查询空引用异常 这里是查询:

var bPermisos = from b in ruc.Permisos
                                 where b.IdUsuario == SelCu.Id
                                 select new {
                                     Id=b.Id,
                                     IdUsuario=b.IdUsuario,
                                     IdPerfil=b.IdPerfil,
                                     Estatus=b.Estatus,
                                     Perfil=b.Cat_Perfil.Nombre,
                                     Sis=b.Cat_Perfil.Cat_Sistema.Nombre

                                 };

如果是一个非常简单的问题,我是一个完全新手对不起。

谢谢!!

4

1 回答 1

0

Silverlight 3 不支持将数据绑定到匿名类型。

您需要创建一个简单的类来放入您的属性。

这是 ValueConverter 技术:

namespace SilverlightApplication55
{
    using System;
    using System.Windows;
    using System.Windows.Data;

    public class NamedPropertyConverter : IValueConverter
    {
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value == null || parameter == null)
        {
            return null;
        }

        var propertyName = parameter.ToString();

        var property = value.GetType().GetProperty(propertyName);

        if (property == null)
        {
            return null;
        }

        return property.GetValue(value, null);
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return DependencyProperty.UnsetValue;        
    }
}
}

然后你把它放在你的 UserControl.Resources 中:

<local:NamedPropertyConverter x:Key="NamedPropertyConverter"/>

这是您想要使用命名参数的地方 - 使用 ConverterParameter 将其传递:

<TextBlock Text="{Binding Converter={StaticResource NamedPropertyConverter}, ConverterParameter=Estatus}"/>
于 2010-03-10T15:55:33.020 回答