首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >对父子组合框ItemSource绑定中的子ItemSource进行排序

对父子组合框ItemSource绑定中的子ItemSource进行排序
EN

Stack Overflow用户
提问于 2013-01-31 03:43:10
回答 2查看 323关注 0票数 0

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

Xaml:

代码语言:javascript
复制
<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:

代码语言:javascript
复制
public Inventory()
{
    Initialize();

    var db = new DataContext();
    var viewModel = new ViewModel();

    DataContext = viewModel;
}

查看模型:

代码语言:javascript
复制
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);
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-02-10 09:02:07

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

代码语言:javascript
复制
var dataOptions = new DataLoadOptions();
dataOptions.AssociateWith<InventoryCategory>(ic => ic.InventoryLists.OrderBy(o => o.InventoryDescription));
db.LoadOptions = dataOptions;
票数 0
EN

Stack Overflow用户

发布于 2013-01-31 03:57:06

试试这个:

代码语言:javascript
复制
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);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14612330

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档