2015-06-02 154 views
1

从它的外表下绑定删除以前的组合框的SelectedItem我有一个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> 

我结合我新上这样

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)和我选择的'B'并单击了添加行 button。在我的下一个组合框列表中,我希望获得Items(A,C,D ) 只要。

由于我使用MVVM和我LstProducts是我产品类我也试过这样的,从这个列表中删除所选的项目,但我也无法让我的名单“LstProducts

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()); 
} 

回答

0

您试图从组合框中删除项目但组合框与列表“LstProducts”绑定尝试在选择项目时删除“LstProducts”中的项目所以它将反映在UI中。像这样

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

这里X是你的ListItems类型。并确保你实现INotifyPropertyChanged

+0

是的,我试过这个通过使用我的代码 obj.LstProducts.Remove(this.combo.SelectedItem.ToString()); –

+0

@SabyasachiMishra我已编辑我的回答尝试一个。 –

+0

并尝试使用Page/window DataContext而不是Buttondatacontext。 –