为什么我在DataTemplate中的层叠下拉菜单不起作用?

时间:2019-04-13 04:38:55

标签: .net wpf

我有一个WPF窗口,其中包含多行组合框,我想进行级联下拉菜单。我定义了绑定和IEnumerable类型,并将其绑定到组合框,但是当我在“第一个”下拉列表中更改值时,第二个下拉列表中不填充这些值。在过去的2天里,我一直在尝试这种方法。有人可以帮忙吗

这是代码

  

XAML

<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"  SelectedValue="{Binding Condition, Mode=TwoWay}"  SelectedItem="{Binding SelectedCondition, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}"  Width="80" Height="23" />

          <ComboBox  x:Name="cmbType" ItemsSource="{Binding TypeList, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}, UpdateSourceTrigger=PropertyChanged}" DisplayMemberPath="Name"  SelectedValuePath="Name" SelectedValue="{Binding Type, Mode=TwoWay}"   Width="80" Height="23" />

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

隐藏代码

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

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

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

private ConditionList _selectedCondition;
public ConditionList SelectedCondition
{
  get { return _selectedCondition; }
  set
  {
    _selectedCondition = value;
    NotifyPropertyChanged();

 if(_selectedCondition.Name == "AND")
    _TypeList = new List<TypeList> { new TypeList() { Name = "1" }, new TypeList() { Name = "2" }, new TypeList() { Name = "3" } };
  else if (_selectedCondition.Name == "OR")
     _TypeList = new List<TypeList> { new TypeList() { Name = "z" }, new TypeList() { Name = "x" }, new TypeList() { Name = "y" } };
    }
 }


private IEnumerable<TypeList> _TypeList;
public IEnumerable<TypeList> TypeList
{
  get { return _TypeList; }
  set { _TypeList = value; NotifyPropertyChanged(); }
}

public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([System.Runtime.CompilerServices.CallerMemberName] String propertyName = "")
{
   if (PropertyChanged != null)
    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
  

模式

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 string Condition { get; set; }
  public string Type { get; set; }
}
  

UI

enter image description here

1 个答案:

答案 0 :(得分:0)

我通过使用MultiBinding实现了这一目标

<ComboBox SelectedValue="{Binding Type}"  Width="80" Height="23" >
    <ComboBox.ItemsSource>
       <MultiBinding Converter="{StaticResource GetTypeList}">
 <Binding ElementName="cmbCondition" Path="SelectedValue" />
       </MultiBinding>
    </ComboBox.ItemsSource>
</ComboBox>