繁体   English   中英

根据另一个组合框选择更改组合框ItemsSource

[英]Change ComboBox ItemsSource based on another ComboBox selection

我有两个ComboBoxes A和B。第一个填充有项目列表。 我需要基于A的SelectedItem更改B的ItemsSource Binding

问题:在A上选择X时,没有填充B。

请注意,我没有过滤ItemsSource ,它是对绑定的完全更改。 myItemSources是ObservableCollections

 <ComboBox Name="ComboBoxB">
    <ComboBox.Style>
      <Style TargetType="{x:Type ComboBox}">
           <Setter Property="ItemsSource" Value="{Binding myItemSource1}" />
              <Style.Triggers>
                <DataTrigger Binding="{Binding SelectedValue, ElementName=ComboBoxA}" Value="X">
                  <Setter Property="ItemsSource" Value="{Binding myItemSource2}" />
                </DataTrigger>
          </Style.Triggers>
      </Style>
    </ComboBox.Style>
</ComboBox>

这里有一个示例,说明如何使用MVVM设计模式实现这种级联的ComboBox: https : //blog.magnusmontin.net/2013/06/17/cascading-comboboxes-in-wpf-using-mvvm/

<ComboBox ItemsSource="{Binding Countries}"
          DisplayMemberPath="Name"
          SelectedItem="{Binding SelectedCountry}" />

<ComboBox ItemsSource="{Binding Cities}"
          DisplayMemberPath="Name"
          SelectedItem="{Binding SelectedCity}"
          Margin="0 5 0 0"/>

public CustomObservableCollection<City> Cities {
  get;
  private set;
}

private Country _selectedCountry;
public Country SelectedCountry {
  get {
    return _selectedCountry;
  }
  set {
    _selectedCountry = value;
    this.Cities.Repopulate(_selectedCountry.Cities);
  }
}
...
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM