代码之家  ›  专栏  ›  技术社区  ›  Duncan Bayne

使用WCF RIA、Entity Framework 4、Silverlight 4进行惯用默认排序?

  •  1
  • Duncan Bayne  · 技术社区  · 15 年前

    我有两个Silverlight4.0组合框;第二个组合框显示在第一个组合框中选择的实体的子项:

    <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" />
    

    视图背后的代码提供了一种(简单的、黑客式的)数据绑定这些组合框的方法,方法是通过WCF RIA服务加载Entity Framework 4.0实体:

    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;
    

    我想做的是让两个组合框按字母顺序对其内容排序,如果可能的话,我想在XAML中指定这种行为。

    有人能告诉我使用SL4/EF4/WCF RIA技术栈的惯用方法是什么吗?

    1 回复  |  直到 15 年前
        1
  •  0
  •   Jehof    15 年前

    尝试使用 CollectionViewSource 把这个绑在你的组合框上。CollectionViewSource提供排序、分组和筛选。

    作为CollectionViewSource的源设置EntitySet。可以将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-->
    

    我没有测试过,但它应该可以用。CollectionViewSource的属性源属于Object类型。不知道该对象是否需要实现指定的接口,如IEnumerable。