List <string>绑定到ComboBox </string>

时间:2013-01-04 19:08:21

标签: c# wpf list binding combobox

我的列表存在问题,该列表绑定到ComboBox

List

private List<string> _CourseList = new List<string>();
public List<string> CourseList
        {
            get { return _CourseList; }
            set
            {
                _CourseList = value;
                OnPropertyChanged("CourseList");
            }
        }

ComboBox

的XAML代码
<ComboBox x:Name="cbxCourse" Height="23" MinWidth="100" Margin="5,1,5,1" VerticalAlignment="Top" ItemsSource="{Binding Path=CourseList}" IsEnabled="{Binding Path=CanExport}" SelectedIndex="{Binding Path=CourseListSelectedIndex}" SelectedItem="{Binding Path=CourseListSelectedItem}" SelectionChanged="cbxCourse_SelectionChanged"/>

现在我从另一个帖子填充List

void Database_LoadCompleted(object sender, SqliteLoadCompletedEventArgs e)
{
    foreach (DataTable Table in DataSetDict[CampagneList[0]].Tables)
    {
        CourseList.Add(Table.TableName);
    }
}

一切看起来都不错,ComboBox改变了它的项目。 当我尝试使用以下内容更新MainThread中的ComboBoxCourseList)时

    private void cbxCampagne_SelectionChanged(object sender, EventArgs e)
    {
        if (cbxCampagne.SelectedItem != null)
        {
            CourseList.Clear();
            foreach (DataTable Table in DataSetDict[CampagneList[_CampagneListSelectedIndex]].Tables)
            {
                CourseList.Add(Table.TableName);
            }
    }

CourseList的所有元素都发生了变化(我可以在Textbox中看到它)但在ComboxBox中没有任何变化。

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

尝试更改CourseListObervableCollection<T>

http://msdn.microsoft.com/en-us/library/ms668604.aspx

Binding仅在设置CourseList时(分配列表时)通知UI,而不是在其内容发生变化时通知UI。

Invoke event on MainThread from worker thread

中的一些代码

这表明List<>一旦分配就不会改变,ObservableList<>会发生变化。

视图模型

//Viewmodel
public class WindowViewModel : INotifyPropertyChanged
{
    private volatile bool _canWork;
    private List<string> _items;
    private ObservableCollection<string> _obervableItems;

    public WindowViewModel()
    {
        //Queue some tasks for adding and modifying the list
        ThreadPool.QueueUserWorkItem(AddItems);
        ThreadPool.QueueUserWorkItem(ModifyItems);

        //Create a background worker to do some work and then we can bind the output to
        //our ObservableList
        var obervableWorker = new BackgroundWorker();
        obervableWorker.DoWork += ObervableWorkerOnDoWork;
        obervableWorker.RunWorkerCompleted += ObervableWorkerOnRunWorkerCompleted;

        obervableWorker.RunWorkerAsync();
    }

    private void ObervableWorkerOnRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs runWorkerCompletedEventArgs)
    {
        var items = ObservableItems as ObservableCollection<string>;

        var workerItems = runWorkerCompletedEventArgs.Result as List<string>;

        foreach (var workerItem in workerItems)
        {
            items.Add(workerItem);
        }

        for (int i = 50; i < 60; i++)
        {
            var item = items.First(x => x == i.ToString());
            items.Remove(item);
        }
    }

    private void ObervableWorkerOnDoWork(object sender, DoWorkEventArgs doWorkEventArgs)
    {
        Thread.Sleep(100);
        int count = 0;
        var items = new List<string>();
        while (100 > count++)
        {
            items.Add(count.ToString());
        }

        doWorkEventArgs.Result = items;
    }

    private void ModifyItems(object state)
    {
        while (!_canWork)
        {
            Thread.Sleep(100);
        }
        var items = Items as List<string>;
        for (int i = 50; i < 60; i++)
        {
            items.RemoveAt(i);
        }
    }

    private void AddItems(object state)
    {
        Thread.Sleep(100);
        int count = 0;
        var items = Items as List<string>;
        while (100 > count++)
        {
            items.Add(count.ToString());
        }
        _canWork = true;
    }

    public IEnumerable<string> Items
    {
        get { return _items ?? (_items = new List<string>()); }
        set { _items = new List<string>(value);
            OnPropertyChanged();
        }
    }

    public IEnumerable<string> ObservableItems
    {
        get { return _obervableItems ?? (_obervableItems = new ObservableCollection<string>()); }
        set { _obervableItems = new ObservableCollection<string>(value); OnPropertyChanged();}
    }

    public event PropertyChangedEventHandler PropertyChanged;

    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

窗口

//Window.Xaml
<Window x:Class="ComboBox.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:comboBox="clr-namespace:ComboBox"
        Title="MainWindow" Height="350" Width="525">
    <Window.DataContext><comboBox:WindowViewModel /></Window.DataContext>
    <Grid>
        <ComboBox Width="200" Height="22" ItemsSource="{Binding Items}"></ComboBox>
        <ComboBox Margin="0,44,0,0" Width="200" Height="22" ItemsSource="{Binding ObservableItems}"></ComboBox>
    </Grid>
</Window>

可以很容易地修改它以使用Dispatcher Invoke:Change WPF controls from a non-main thread using Dispatcher.Invoke