簡體   English   中英

使用WCF RIA,實體框架4,Silverlight 4的慣用默認排序?

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

我有兩個Silverlight 4.0 ComboBox; 第二個顯示第一個中選擇的實體的子代:

<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實體,提供了一種(簡單,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;

我想要做的是讓兩個組合框都按字母順序對它們的內容進行排序,並且我想盡可能在​​XAML中指定該行為。

有人可以告訴我使用SL4 / EF4 / WCF RIA技術堆棧的慣用方式是什么?

嘗試使用CollectionViewSource並將其綁定到您的組合框。 CollectionViewSource提供排序,分組和過濾。

設置EntitySet作為CollectionViewSource的Source。 可以將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的Property Source是object類型。 不知道該對象是否需要實現指定的接口,例如IEnumerable。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM