2010-03-10 31 views
0

我有一個匿名的linq查詢,我綁定到一個數據網格,當我調試它帶來好的數據,但它不顯示在數據網格中,我懷疑在將其綁定到數據網格之前,對RIA服務的請求未完成。我可以使用LoadOperation <>()完成的事件。但它只適用於定義實體,所以我該怎麼做? 僅供參考這裏的最後一個職位: LINQ query null reference exception 下面是該查詢:Silverlight的DataGrid不顯示任何數據與匿名查詢RIA服務

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 

           }; 

我是一個完全新手,對不起,如果是一個很簡單的問題。

謝謝!

回答

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}"/> 
+0

在DomainService中還是在同一個xaml.cs中?我是否必須爲每個需要的匿名查詢創建每個類? – user289082 2010-03-10 15:59:38

+0

您只需要爲需要綁定的對象創建類。您也可以使用ValueConverter和ConverterParamter通過反射來提取屬性值。 – 2010-03-10 19:41:56

+0

= S我對反射一無所知可以寫一個片段,因爲我可以更好地理解它。謝謝! – user289082 2010-03-11 15:56:46