【问题标题】:Idiomatic default sort using WCF RIA, Entity Framework 4, Silverlight 4?使用 WCF RIA、Entity Framework 4、Silverlight 4 的惯用默认排序?
【发布时间】:2010-03-25 07:07:38
【问题描述】:

我有两个 Silverlight 4.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 实体,提供了一种(简单、笨拙的)数据绑定这些 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;

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

谁能告诉我使用 SL4 / EF4 / WCF RIA 技术堆栈执行此操作的惯用方式是什么?

【问题讨论】:

    标签: .net silverlight entity-framework wcf-ria-services


    【解决方案1】:

    尝试使用CollectionViewSource 并将其绑定到您的组合框。 CollectionViewSource 提供排序、分组和过滤。

    作为 CollectionViewSource 的 Source 设置您的 EntitySet。 CollectionViewSource 可以添加到任何控件的 Resources-Section。

    <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 的 Property Source 是 object 类型的。不知道该对象是否需要实现指定的接口,例如 IEnumerable。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-07
    • 2011-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多