0

我有一个用户控件,其中包含两个组合框,用户可以在其中选择一个库存类别,然后选择一个库存项目。库存类别组合框的选定值触发库存项目组合框列表。我遇到的问题是我无法对库存项目组合列表进行排序。当我将组合的库存类别和库存项目添加到 DataContext 时,我可以对库存类别进行排序,但是我无法对库存项目列表进行排序。代码如下所示:

xml:

<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);
}
4

2 回答 2

0

试试这个:

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);
}
于 2013-01-30T19:57:05.733 回答
0

您必须使用 .AssociateWith<> 而不是 .LoadWith<>

var dataOptions = new DataLoadOptions();
dataOptions.AssociateWith<InventoryCategory>(ic => ic.InventoryLists.OrderBy(o => o.InventoryDescription));
db.LoadOptions = dataOptions;
于 2013-02-10T01:02:07.457 回答