[英]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.