-1

我做的一切对吗?

public List<string> combolist { get; set; }

...

this.combolist = new List<string>();
MySqlCommand status_db = new MySqlCommand("select name_ru from request_status", conn);
MySqlDataReader combodata = status_db.ExecuteReader();
   while (combodata.Read())
   {
     combolist.Add(combodata.GetString(0));
   }
   this.DataContext = this;

在xml中:

<ComboBox ItemsSource="{Binding Path=combolist}"... 

但是项目组合框是空的,怎么了?

4

2 回答 2

0

您应该使用 ObservableCollection。在调用 InitializeComponent() 之前实例化 ObservableCollection

于 2012-07-10T13:28:28.087 回答
0

这有效:

用户控件1.xaml

<Grid>
    <ComboBox ItemsSource="{Binding Combolist}" />
</Grid>

usercontrol1.xaml.cs

public partial class UserControl1 : UserControl
{
    public ObservableCollection<string> Combolist { get; private set; }

    public UserControl1()
    {
        this.Combolist = new ObservableCollection<string>();//just initialize once!
        InitializeComponent();
        //if you wanna load new data, call .Clear() before
        //this.Combolist.Clear();
        //MySqlCommand status_db = new MySqlCommand("select name_ru from request_status", conn);
        //MySqlDataReader combodata = status_db.ExecuteReader();
        //while (combodata.Read())
        //{
        //    Combolist.Add(combodata.GetString(0));
        //}

        //test
        this.Combolist.Add("qqqq");
        this.Combolist.Add("wwww");
        this.Combolist.Add("eeee");
        this.Combolist.Add("rrrr");

        this.DataContext = this;
    }
}
于 2012-07-10T13:41:28.177 回答