c# - 通过 List 从其下一个外观绑定(bind)中删除以前的 ComboBox SelectedItem

标签 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>

我正在绑定(bind)我新创建的 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)并且我选择了 'B' 并点击 添加行 按钮。在我的下一个组合框列表中,我希望只获取 Items(A,C,D)。

当我使用 MVVM 和我的 Lst产品 在我的产品 我也试过这样从这个列表中删除选定的项目,但我也无法得到我的列表' Lst产品 '
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());
}

最佳答案

您正在尝试从组合框中删除项目但组合框与列表“LstProducts”绑定(bind)在选择项目时尝试删除“LstProducts”中的项目,以便它将反射(reflect)在 UI 中。像这样

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

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

关于c# - 通过 List 从其下一个外观绑定(bind)中删除以前的 ComboBox SelectedItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30589887/

相关文章:

c# - ItemsControl 和 Canvas 中的多个数据模板

c# - 自定义对话框窗口 Prism

c# - 使用数据集的 Crystal Reports 子报表

wpf - 如何在没有 Prism 的情况下在 WPF 中实现区域?

c# - 尝试使用 Entity Framework 保存更改时出现异常

wpf - 删除用户控件上的焦点矩形

c# - 我应该如何在线程之间同步?

c# - 使用 LINQ 将两个不同类型的 IQueryable 变量连接在一起

c# - RaisePropertyChanged 在 TwoWay Bound 属性上抛出 StackOverflow 异常

c# - WCF 和 Silverlight CrossDomain.xml