我有一个用户控件,它包含两个组合框,用户可以在其中选择库存类别,然后选择库存项目。库存类别组合框的选定值将触发库存项目组合框列表。我遇到的问题是我不能对库存物品组合列表进行排序。当我将组合的库存类别和库存项目添加到DataContext时,我可以对库存类别进行排序,但不能对库存项目列表进行排序。代码如下所示:
Xaml:
<ComboBox Style="{DynamicResource ResourceKey=ComboBoxStyle}"
Name="cboInventoryCategory"
MinWidth="250"
HorizontalAlignment="Stretch"
ItemsSource="{Binding DataContext.InventoryCategoryList, RelativeSource={RelativeSource AncestorType=UserControl,Mode=FindAncestor}}"
DisplayMemberPath="Category"
SelectedValuePath="Id"
SelectionChanged="cboInventoryCategory_SelectionChanged" />
<ComboBox Style="{DynamicResource ResourceKey=ComboBoxStyle}"
Name="cboInventoryItem"
MinWidth="250"
HorizontalAlignment="Stretch"
ItemsSource="{Binding ElementName=cboInventoryCategory,Path=SelectedItem.InventoryLists}"
SelectedValue="{Binding Inventory_Id, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
DisplayMemberPath="InventoryDescription"
SelectedValuePath="InventoryId"
SelectionChanged="cboRequestFor_SelectionChanged" />Xaml.CS:
public Inventory()
{
Initialize();
var db = new DataContext();
var viewModel = new ViewModel();
DataContext = viewModel;
}查看模型:
using System.Linq;
using System.Data.Linq;
public IEnumerable<InventoryCategory> InventoryCategoryList { get; set; }
//InventoryCategory is a table
//InventoryList is a view and has a primary key and an association in the dbml
public ViewModel()
{
InventoryCategoryList_Refresh()
}
public void InventoryCategoryList_Refresh()
{
var dataOptions = new DataLoadOptions();
dataOptions.LoadWith<InventoryCategory>(ic => ic.InventoryLists);
db.LoadOptions = dataOptions;
InventoryCategoryList = db.InventoryCategories.Where(w => w.Active == true).OrderBy(o => o.Category);
}发布于 2013-02-10 09:02:07
必须使用.AssociateWith<>而不是.LoadWith<>
var dataOptions = new DataLoadOptions();
dataOptions.AssociateWith<InventoryCategory>(ic => ic.InventoryLists.OrderBy(o => o.InventoryDescription));
db.LoadOptions = dataOptions;发布于 2013-01-31 03:57:06
试试这个:
public void InventoryCategoryList_Refresh()
{
var dataOptions = new DataLoadOptions();
dataOptions.LoadWith<InventoryCategory>(ic => ic.InventoryLists);
db.LoadOptions = dataOptions;
InventoryCategoryList = db.InventoryCategories.Where(w => w.Active == true).OrderBy(o => o.Category).ThenBy(c=>c.InventoryItems);
}https://stackoverflow.com/questions/14612330
复制相似问题