通过List从下一个外观绑定中删除以前的ComboBox SelectedItem

时间:2015-06-02 07:25:42

标签: c# wpf mvvm

我有ComboBoxTextBox喜欢

<TextBlock Grid.Row="0" Grid.Column="0" TextAlignment="Center" VerticalAlignment="Center" 
FontSize="16" FontWeight="Bold">Products :</TextBlock>

<ComboBox Grid.Row="0" Grid.Column="1" Margin="10" Height="30"  
ItemsSource="{Binding Path=LstProducts}" SelectedItem="{Binding Path=Items.Product}"/>

<TextBlock Grid.Row="0" Grid.Column="2" TextAlignment="Center" VerticalAlignment="Center" 
FontSize="16" FontWeight="Bold">Cost :</TextBlock>

<TextBox Grid.Row="0" Grid.Column="3" Margin="10" Height="30"></TextBox>

每次我在Button点击

上创建这些控件时
<Button Grid.Row="1" Grid.Column="1" Margin="15"  VerticalAlignment="Center" FontSize="12" 
FontWeight="Bold" Click="ButtonBase_OnClick" Name="BtnAddmore">Add Rows</Button>

我在按钮点击事件上绑定了我新创建的ComboBox,如下所示

var ddlCurriency = new ComboBox
 {
     FontWeight = FontWeights.Bold,
     Margin = new Thickness(7),
     BorderBrush = Brushes.Black,
     Name = "NewDdlCurrency" + _btncount
}; 
ddlCurriency.SetBinding(ItemsControl.ItemsSourceProperty, new Binding("Items.Product"));

我不希望我的先前组合框选择的项目在列表中重复。

例如。 假设我的第一个Combobox包含项目列表(A,B,C,D),我选择&#39; B&#39; 并点击添加行按钮。在我的下一个组合框列表我希望只获得项目(A,C,D)。

当我使用MVVM并且我的 LstProducts 位于我的产品类中时,我也尝试过从此列表中删除所选项目,但我也无法获取我的清单&#39; LstProducts &#39;

private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
   Button button = sender as Button;
   Product obj = button.DataContext as Product;
   obj.LstProducts.Remove(this.combo.SelectedItem.ToString());
}

1 个答案:

答案 0 :(得分:0)

您正在尝试从组合框中删除项目但是组合框与List&#34; LstProducts&#34;尝试删除&#34; LstProducts&#34;中的项目。选择项目时,它将反映在UI中。喜欢这个

var Item=combobox.selectedItem as x;
if(LstProducts.Contains(Item))
LstProducts.remove(Item);

这里X是你的ListItems类型。并确保您已实施INotifyPropertyChanged