简体   繁体   English

使用WCF RIA,实体框架4,Silverlight 4的惯用默认排序?

[英]Idiomatic default sort using WCF RIA, Entity Framework 4, Silverlight 4?

I've got two Silverlight 4.0 ComboBoxes; 我有两个Silverlight 4.0 ComboBox; the second displays the children of the entity selected in the first: 第二个显示第一个中选择的实体的子代:

<ComboBox 
    Name="cmbThings"
    ItemsSource="{Binding Path=Things,Mode=TwoWay}"
    DisplayMemberPath="Name"
    SelectionChanged="CmbThingsSelectionChanged" />
<ComboBox 
    Name="cmbChildThings"
    ItemsSource="{Binding Path=SelectedThing.ChildThings,Mode=TwoWay}"
    DisplayMemberPath="Name" />

The code behind the view provides a (simple, hacky) way to databind those ComboBoxes, by loading Entity Framework 4.0 entities through a WCF RIA service: 视图后面的代码通过通过WCF RIA服务加载Entity Framework 4.0实体,提供了一种(简单,hacky)方式对这些ComboBox进行数据绑定:

public EntitySet<Thing> Things { get; private set; }
public Thing SelectedThing { get; private set; }

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    var context = new SortingDomainContext();
    context.Load(context.GetThingsQuery());
    context.Load(context.GetChildThingsQuery());
    Things = context.Things;            
    DataContext = this;
}

private void CmbThingsSelectionChanged(object sender, SelectionChangedEventArgs e)
{
    SelectedThing = (Thing) cmbThings.SelectedItem;
    if (PropertyChanged != null)
    {
        PropertyChanged.Invoke(this, new PropertyChangedEventArgs("SelectedThing"));
    }
}

public event PropertyChangedEventHandler PropertyChanged;

What I'd like to do is have both combo boxes sort their contents alphabetically, and I'd like to specify that behaviour in the XAML if at all possible. 我想要做的是让两个组合框都按字母顺序对它们的内容进行排序,并且我想尽可能在​​XAML中指定该行为。

Could someone please tell me what is the idiomatic way of doing this with the SL4 / EF4 / WCF RIA technology stack? 有人可以告诉我使用SL4 / EF4 / WCF RIA技术堆栈的惯用方式是什么?

Try to use a CollectionViewSource and bind this to your combobox. 尝试使用CollectionViewSource并将其绑定到您的组合框。 The CollectionViewSource provides sorting, grouping and filtering. CollectionViewSource提供排序,分组和过滤。

As Source for your CollectionViewSource set your EntitySet. 设置EntitySet作为CollectionViewSource的Source。 The CollectionViewSource can be added to the Resources-Section of any control. 可以将CollectionViewSource添加到任何控件的“资源”部分。

<CollectionViewSource Source="{StaticResource Things}" x:Key="cvs"> <!--The source can be set in procedural code-->
  <CollectionViewSource.SortDescriptions>
    <scm:SortDescription PropertyName="Name"/> <!--The name of the property to sort items-->
  </CollectionViewSource.SortDescriptions>
</CollectionViewSource>

<!--The prefix scm mappes to the System.ComponentModel-->

I didn´t tested it, but it should work. 我没有测试过,但是应该可以。 The Property Source of the CollectionViewSource is of type object. CollectionViewSource的Property Source是object类型。 Don´t know if that object needs to implement a specified interface, such as IEnumerable. 不知道该对象是否需要实现指定的接口,例如IEnumerable。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM