Witam
Mam mały problem.
Stworzyłem sobie własną kontrolkę (ona będzie bardziej rozbudowana, ale tutaj ją uprościłem, aby wyjaśnić w czym leży problem).
Zawiera ona tylko ComboBoxa,
Oraz mam główne okno programu w którym umieściłem **DWIE **swoje kontrolki.
Kod kontrolki wygląda tak:
UserControl1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace charcik
{
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
private void UserControl1_Load(object sender, EventArgs e)
{
comboBox1.DisplayMember = "nazwa";
comboBox1.ValueMember = "ID";
comboBox1.DataSource = DB.Kolory;
comboBox1.SelectedIndex = -1;
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}
Jest jeszcze plik DB.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace charcik
{
public class Kolor
{
public int ID { get; set; }
public string nazwa { get; set; }
}
public static class DB
{
private static List<Kolor> kolory;
static DB()
{
kolory = new List<Kolor>()
{
new Kolor() { ID=1, nazwa = "zolty"},
new Kolor() { ID=2, nazwa = "zielony"},
new Kolor() { ID=3, nazwa = "niebieski"},
};
}
public static List<Kolor> Kolory
{
get
{
return kolory;
}
}
}
}
Jak widać Combobox pobiera itemy właśnie z tej 'bazy'
Niestety przy zmianie itemu w Comboboxie1 zmienia się item w Comboboxie2
Obrazuje to ten filmik:
W jaki sposób sobie z tym poradzić ?
Oczywiście jeśli do Comboboxa dodałbym itemy "normalnie", a nie przez DataSource to nie ma takiego problemu.