views:

311

answers:

1

Hi all! I have an anonymous linq query that I bind to a datagrid, when I debug it brings alright the data but it doesn't show in the datagrid, I suspect that the request to RIA services isn't completed before I bound it to the datagrid. I could use the LoadOperation<>() Completed event. But it only works with Defined Entities so how can I do that? For reference here is the last post: http://stackoverflow.com/questions/2403903/linq-query-null-reference-exception Here is the query:

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

                                 };

I'm a totally newbie sorry if is a very simple question.

Thanks!!

A: 

Silverlight 3 doesn't support data binding to anonymous types.

You need to create a simple class to put your properties into.

Here is the ValueConverter technique:

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

Then you put this in your UserControl.Resources:

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

And this where you want to use a named parameter - pass it in with the ConverterParameter:

<TextBlock Text="{Binding Converter={StaticResource NamedPropertyConverter}, ConverterParameter=Estatus}"/>
Michael S. Scherotter
in the DomainService or in the same xaml.cs? And do I have to create every class for each anonymous query I need?
you only need to create classes for objects you need to bind to. You could also do this with a ValueConverter and a ConverterParamter to extract the property value through reflection.
Michael S. Scherotter
=S I don't know anything about reflection could you write a snippet for I can understand it better plz? Thank you!