我有一个 ComboBox
和 TextBox
像
<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/