C#具有相同项目的多个组合框 [英] C# multiple combobox with the same itemssource

查看:59
本文介绍了C#具有相同项目的多个组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个组合框,两者的itemsourse相同。

I have two comboboxes and the itemssourse for both is the same.

List<string> cars = new List<string>();
cars.Add("Audi");
cars.Add("BMW");
cars.Add("Mercedes-Benz");

this.ComboBox1.ItemsSource = cars;
this.ComboBox2.ItemsSource = cars;

假设我在 ComboBox1 。我想要的是在 ComboBox1 中选择 Audi时,删除/禁用 ComboBox2 中的 Audi。

Let's say that I have selected "Audi" in ComboBox1. What I want is when "Audi" is selected in ComboBox1 remove/disable "Audi" in ComboBox2.

有人可以帮我吗? (我是c#/ wpf编程的新手)

Could some one help me with that? (I'm new to c#/wpf programing)

推荐答案

在公共区域中定义2个列表

define 2 list in public like

List<string> cars = new List<string>();
List<string> cars2 = new List<string>();

public CarsView()
    {
        InitializeComponent();

        cars.Add("Audi");
        cars.Add("BMW");
        cars.Add("Mercedes-Benz");

        this.ComboBox1.ItemsSource = cars;
        this.ComboBox2.ItemsSource = cars;
    }

您的函数必须是这样

private void ComboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ComboBox2.SelectedIndex = -1;
            string cb1 = ComboBox1.SelectedValue as string;
            cars2.Clear();
            cars2.AddRange(cars);
            cars2.Remove(cb1);
            ComboBox2.ItemsSource = null;
            ComboBox2.ItemsSource = cars2;
        }

这篇关于C#具有相同项目的多个组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆