如何在DataTemplate中构建级联下拉菜单?

时间:2019-04-12 13:04:27

标签: wpf

我正在构建WPF表单,我想在其中实现ItemsControl内的级联下拉菜单。会有多行,并且Dropdown2源依赖于Dropdown1。 当我在FilterData中添加一个项目时,该行就会被添加,并且下拉列表也会填充,但是我不确定如何创建多行的关系数据库

这是我到目前为止尝试过的

<ItemsControl Grid.Row="1" x:Name="Filter" ItemsSource="{Binding FilterData}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <StackPanel Margin="10" Orientation="Horizontal" >
                        <CheckBox IsChecked="{Binding Group}"/>
                        <ComboBox x:Name="cmbCondition" ItemsSource="{Binding ConditionList, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}"
                                  DisplayMemberPath="Name"  SelectedValuePath="Name" SelectedItem="{Binding ConditionList}" Width="80" Height="23" />
                        <ComboBox  x:Name="cmbType" ItemsSource="{Binding TypeList, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}"
                                  DisplayMemberPath="Name"  SelectedValuePath="Name" SelectedItem="{Binding TypeList}" Width="80" Height="23" />
                        <ComboBox  x:Name="cmbOperator" ItemsSource="{Binding OperatorList, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}"
                                  DisplayMemberPath="Name"  SelectedValuePath="Name" SelectedItem="{Binding OperatorList}" Width="80" Height="23" />


                    </StackPanel>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>


  public ObservableCollection<FilterData> _FilterData { get; set; }
        public ObservableCollection<ConditionList> _ConditionList { get; set; }
        public ObservableCollection<TypeList> _TypeList { get; set; }

 public IEnumerable<FilterData> FilterData
        {
            get { return _FilterData; }
        }

        public IEnumerable<ConditionList> ConditionList
        {
            get { return _ConditionList; }
        }

        public IEnumerable<TypeList> TypeList
        {
            get { return _TypeList; }
        }

//Form Load event
 Filter.DataContext = this;
            AddRow();

private void AddRow()
        {
            _ConditionList = new ObservableCollection<ConditionList>()
            {
                new ConditionList() { Name = "AND" },
                new ConditionList() { Name = "OR" }
            };

            _FilterData.Add(new FilterData
            {
                Group = true,
            //    Condition = _ConditionList
            });


        }

// Modal

public class TypeList
    {
        public string Name { get; set; }
    }

    public class ConditionList
    { 
        public string Name { get; set; }
    }

    public class FilterData
    {
        public bool Group { get; set; }
        public ConditionList Condition { get; set; }
    }

1 个答案:

答案 0 :(得分:1)

如果将第一个SelectedItem的{​​{1}}属性绑定到类型ComboBox的源属性,则可以填充第二个ConditionList的源集合({{ 1}})。

请确保实现INotifyPropertyChanged接口,并为正在设置为新值的源集合属性引发ComboBox事件,例如:

TypeList

XAML:

PropertyChanged