两个CombBox绑定到对象列表中的对象列表

时间:2015-01-23 14:46:08

标签: c# wpf xaml

我有一个绑定到组合框(Master组合框)的Itemsource的集合。在这里面是另一个集合,它绑定到一个永远不会填充的ItemControl中的另一个combox。请帮忙

XAML

<ComboBox ItemsSource="{Binding NodeNames}" DisplayMemberPath="name" 
                                          SelectedValue="{Binding ConditionalNodeId, Mode=TwoWay}" SelectedValuePath="id">

</ComboBox>
  </WrapPanel>
    <ItemsControl ItemsSource="{Binding ConditionList}" AlternationCount="{Binding ConditionList.Count}">
      <ItemsControl.ItemTemplate>
        <DataTemplate>
          <WrapPanel>
            <ComboBox ItemsSource="{Binding NodeMembershipFunction}">

            </ComboBox>
          </WrapPanel>
        </DataTemplate>
      </ItemsControl.ItemTemplate>
    </ItemsControl>

班级代码

public class ConditionsVM : INotifyPropertyChanged
{
    public ObservableCollection<ConditionsRuleModel> _ConditionList { get; private set; }
    public List<MembershipFunction> _NodeMembershipFunction { get; private set; }        

    public ConditionsVM()
    {
        _ConditionList = new ObservableCollection<ConditionsRuleModel>();
        _NodeMembershipFunction = new List<MembershipFunction>();

        List<MembershipFunction> m = new List<MembershipFunction>();
        foreach (MembershipFunction mf in ConditionNode.membershipFunctions)
        {
           m.Add(mf);
        }
        NodeMembershipFunction = m;
    }

    public List<MembershipFunction> NodeMembershipFunction
    {
        get
        {
            return _NodeMembershipFunction;
        }
        set
        {
            if (_NodeMembershipFunction != value)
            {
                _NodeMembershipFunction = value;


            }
        }
    }

1 个答案:

答案 0 :(得分:1)

您似乎假设第二个组合框具有与第一个组合框相同的DataContext。事实并非如此,第二个组合框的DataContext是来自ConditionList的单个元素,即ConditionRuleModel。

为第二个组合框解决它的一种方法是:

    <ComboBox ItemsSource="{Binding DataContext.NodeMembershipFunction, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}, AncestorLevel=2}}"/>

我怀疑你需要AncestorLevel为2,因为ComboBox本身也是一个ItemsCOntrol。不确定祖先查找是否从元素本身开始。