我有两个组合框具有相同的项目。 我想通过索引获取ComboBox的ComboBoxItem,但正在返回NULL值。 我的代码是:GetItemAt返回null值而不是ComboBoxItem
var index = comboBox1.SelectedIndex;
ComboBoxItem item = comboBox2.Items.GetItemAt(index) as ComboBoxItem; // item is null here
//item = (ComboBoxItem)comboBox2.ItemContainerGenerator.ContainerFromItem(comboBox1.SelectedItem);
//also tried above line but same result(null)
和XAML:
<ComboBox Name="comboBox1" ItemsSource="{Binding ExistingModuleGroups}" SelectedItem="{Binding SelectedModuleGroup}" SelectionChanged="ComboBox1_SelectionChanged">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<ComboBox Name="comboBox2" ItemsSource="{Binding ExistingModuleGroups}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.AddedItems.Count > 0)
{
if (comboBox2.Items.Count > 0)
{
var index = comboBox1.SelectedIndex;
ComboBoxItem item = comboBox2.Items.GetItemAt(index) as ComboBoxItem; // item is null here
//item.IsEnabled = false;
}
}
}
任何想法...
您选择从组合框“1”指标,并试图从组合框位于该索引得到一个项目“2” - 这是由设计? – Krishna
是@Krishna。它的设计。 – AmanVirdi
并且您检查了该索引是否小于combobox2.Items.counts的计数? – Krishna