将集合绑定到ListBox

时间:2017-05-04 16:37:30

标签: c# wpf xaml data-binding

我尝试做一些简单的任务(我猜是这样)。我想从for循环中动态更改GUI 我们来看看我的XAML:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel Name="MyPanel">
        <TextBlock Text="{Binding MyValue}"></TextBlock>
        <Button Click="Button_Click">OK</Button>
        <ListBox Name="myList" ItemsSource="{Binding MyCollection}" >
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid Margin="10">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="20"/>
                            <ColumnDefinition Width="20"/>
                        </Grid.ColumnDefinitions>
                        <TextBlock Text="{Binding A}" Grid.Column="0"/>
                        <TextBlock Text="{Binding B}" Grid.Column="1"/>
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </StackPanel>
</Window>

正如您所看到的,我有显示数字的Textblock,启动程序的按钮和列表框,应该显示收集项目。

单击按钮后,第一个文本块(bindes MyValue)显示动态值,但在列表框中我收到下一个错误:
“这种类型的CollectionView不支持从与Dispatcher线程不同的线程更改其SourceCollection。” 我看到了错误的另一个答案,但无法理解如何在我的情况下实现它。

这里是C#代码:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public static MyModel m;
        public MainWindow()
        {
            m = new MyModel();
            InitializeComponent();
            MyPanel.DataContext = m;
        }

        bool flag = false;
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            flag = !flag;
            Task.Factory.StartNew(() =>
            {
                for (int i = 0; i < 5000000; i++)
                {
                    if (flag == false) break;
                    m.MyValue = i.ToString();
                    m.MyCollection.Add(new ChartPoint { A = i, B = 2 * i });
                }
            });
        }
    }

    public class MyModel : INotifyPropertyChanged
    {
        private string myValue;


        public ObservableCollection<ChartPoint> MyCollection { get; set; }

        public MyModel()
        {
            MyCollection = new ObservableCollection<ChartPoint>();
        }

        public string MyValue
        {
            get { return myValue; }
            set
            {
                myValue = value;
                RaisePropertyChanged("MyValue");
            }
        }

        private void RaisePropertyChanged(string propName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propName));
        }
        public event PropertyChangedEventHandler PropertyChanged;
    }

    public class ChartPoint
    {
        public int A { get; set; }
        public int B { get; set; }
    }
}

非常感谢!

1 个答案:

答案 0 :(得分:1)

我已经改变了一下Button_Click代码,这是你想要实现的,请建议:

MyCollection.find(function(err, result) {
  console.log(result);
});