0

我的列表有问题,该列表绑定到ComboBox.

List:_

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

的 XAML 代码ComboBox

<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 中的ComboBox( ) 时:CourseList

    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(我可以在 a 中看到它Textbox)但ComboxBox没有任何反应。

有任何想法吗?

4

1 回答 1

1

CourseList尝试更改ObervableCollection<T>

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

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

来自工作线程的 MainThread 上的 Invoke 事件的一些代码

这表明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:使用 Dispatcher.Invoke从非主线程更改 WPF 控件

于 2013-01-04T19:14:47.290 回答