为什么我在DataTemplate中的Cascading Dropdown不起作用?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了为什么我在DataTemplate中的Cascading Dropdown不起作用?相关的知识,希望对你有一定的参考价值。

我有一个带有多行Comboboxes的WPF窗口,我想制作级联下拉列表。我已经定义了绑定和IEnumerable类型并将其绑定到Combobox,但是当我更改First下拉列表中的值时,第二个下拉列表不会填充值。过去两天我一直在尝试这个。有人可以在这帮忙

这是代码

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

答案

我通过使用MultiBinding实现了这一点

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

以上是关于为什么我在DataTemplate中的Cascading Dropdown不起作用?的主要内容,如果未能解决你的问题,请参考以下文章

使用动态项绑定到Listbox的DataTemplate中的控件DependencyProperty不起作用

WPF中Control Template和DataTemplate的区别

WPF 中 DataTemplate 中的 x:Key、x:Name 和 x:UID 有啥区别?

绑定到 DataTemplate 中的视图模型属性

WPF为没有DataTemplate属性的控件创建DataTemplate

WPF DataTemplate ComboBox 绑定问题