System.Linq.GroupBy重点在Silverlight不具约束力

问题描述:

list.ItemsSource=db.Templates.GroupBy(t=>t.CategoryName);

在XAML:

<DataTemplate>
   <TextBlock Text="{Binding Key}" />
</DataTemplate>

此代码后。不要显示TextBlock的任何文本。我改变了文本这样的结合

After this code. Don't show any text in TextBlock. I'm changing Text binding like this

<DataTemplate>
   <TextBlock Text="{Binding}" />
</DataTemplate>



的TextBlock这样的 System.Linq.Lookup ^ 2 +分组[显示的文字System.String,Model.Template]

我调试和检查属性。这不是空。

I'm debugging and checking Key property. this is not null.

为什么不绑定的TextBlock?

Why Key don't bind in TextBlock?

如何显示在文本块组标题

How to show group title in Textblock?

嗯? - 不幸的。究其原因是因为 GROUPBY()调用的结果是 System.Linq.Lookup&LT的实例;,&GT; .Grouping 分组是一个嵌套类的查找&LT的;,&GT; 类然而分组, code>标记为内部

Hmmm - unfortunate. The reason is because the result of the GroupBy() call is an instance of System.Linq.Lookup<,>.Grouping. Grouping is a nested class of the Lookup<,> class, however Grouping is marked as internal.

安全在Silverlight的限制,不要让你绑定到非公共类型定义的属性,即使这些属性在一个公共接口,类实现的声明。事实上,你要绑定的对象实例是一个非公开的具体类型意味着你只能绑定到任何公开的基地该类型的类的定义的公共属性。

Security restrictions in Silverlight don't let you bind to properties defined on non-public types, even if those properties are declared in a public interface which the class implements. The fact that the object instance you are binding to is of a non-public concrete type means that you can only bind to public properties defined on any public base classes of that type.

您可以建立一个公共垫片类充当分组视图模型:

You could build a public shim class to act as a view model for the grouping:

public class MyGrouping {
  public string Key {get; internal set;}
}

list.ItemsSource=db.Templates.GroupBy(t=>t.CategoryName)
                             .Select(g => new MyGrouping { Key = g.Key });