Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# DataGrid生成空行_C#_Wpf_Multithreading_Datagrid - Fatal编程技术网

C# DataGrid生成空行

C# DataGrid生成空行,c#,wpf,multithreading,datagrid,C#,Wpf,Multithreading,Datagrid,我有两个线程——让我们把它们命名为Calc线程和UI线程。在Calc线程中,我刷新了一个可观察的集合。我还有一个用于ObservableCollection的CollectionCHanged事件的处理程序。正如我所知,处理程序在引发CollectionChanged事件的同一个线程中执行——因此,在我的例子中,就是刷新ObservableCollection的同一个线程。所以,要刷新UI,我不能像在单线程应用程序中那样直接使用绑定—UI必须通过Dispatcher手动刷新。但当我在UI中使用D

我有两个线程——让我们把它们命名为Calc线程和UI线程。在Calc线程中,我刷新了一个可观察的集合。我还有一个用于ObservableCollection的CollectionCHanged事件的处理程序。正如我所知,处理程序在引发CollectionChanged事件的同一个线程中执行——因此,在我的例子中,就是刷新ObservableCollection的同一个线程。所以,要刷新UI,我不能像在单线程应用程序中那样直接使用绑定—UI必须通过Dispatcher手动刷新。但当我在UI中使用DataGrid时,我会得到空行而不是任何数据,例如,当我使用ListBox时,会显示相应的数据:

左侧为数据网格大小写,右侧为列表框大小写

(列表框只是数据绑定和显示的示例;我不希望数据像在这个列表框中一样显示,而是像在数据网格中一样显示(如果它像我预期的那样工作-而不是像图片上的情况那样)-带列标题的表)

好的,我准备了代码,您可以复制并粘贴代码来重建问题:

C#

使用System.Collections.ObjectModel;
使用System.Collections.Specialized;
使用系统线程;
使用System.Windows;
命名空间WpfApplication1
{
公共类MyClass
{
公共整数{get;set;}
公共字符串Str{get;set;}
}    
公共部分类主窗口:窗口
{
公共ObservableCollection MyCollection{get;set;}
公共主窗口()
{
初始化组件();
MyCollection=新的ObservableCollection();
MyCollection.CollectionChanged+=MyCollection\u CollectionChanged;
线程t=新线程(新线程开始(()=>
{
对于(int i=0;i<10;i++)
{
添加(新的MyClass()
{
整数=i,
Str=“String”+i
});
睡眠(500);
}
}));
t、 Start();
}
void MyCollection\u collection已更改(
对象发送器,
NotifyCollectionChangedEventArgs(e)
{
调度程序。调用(
() =>
{
foreach(e.NewItems中的var项)
添加((MyClass)项);
});
}
}
}
XAML(只需注释/取消注释列表框大小写和数据网格大小写):


这就是你想要的吗

C#

命名空间WpfApplication1
{
公共类MyClass
{
公共整数{get;set;}
公共字符串Str{get;set;}
}
公共部分类主窗口:窗口
{
公共ObservableCollection MyCollection{get;set;}
公共主窗口()
{
初始化组件();
DataContext=this;
MyCollection=新的ObservableCollection();
线程t=新线程(新线程开始(()=>
{
对于(int i=0;i<10;i++)
{
Dispatcher.Invoke(新操作(()=>
{
添加(新的MyClass()
{
整数=i,
Str=“String”+i
});
}));
}
}));
t、 Start();
}
}
}
XAML


其他方法是使用另一个列表:

public partial class MainWindow : Window
    {
        private List<MyClass> _MyCollection;
        public ObservableCollection<MyClass> MyCollection { get; set; }

        private DispatcherTimer dispatcherTimer = new DispatcherTimer();

        public MainWindow()
        {
            InitializeComponent();
            DataContext = this;
            MyCollection = new ObservableCollection<MyClass>();
            _MyCollection = new List<MyClass>();
            dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 500);
            dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);

            Thread t = new Thread(new ThreadStart(() =>
            {
                for (int i = 0; i < 10; i++)
                {
                    _MyCollection.Add(new MyClass()
                    {
                        Integer = i,
                        Str = "String " + i
                    });
                    Thread.Sleep(500);
                }
            }));

            t.Start();
            dispatcherTimer.Start();
        }

        private void dispatcherTimer_Tick(object sender, EventArgs e)
        {
            if (_MyCollection.Count != MyCollection.Count)
            {
                MyCollection.Add(_MyCollection[_MyCollection.Count - 1]);
            }
        }
    }
公共部分类主窗口:窗口
{
私人名单(我的收藏);;
公共ObservableCollection MyCollection{get;set;}
private DispatcherTimer DispatcherTimer=新DispatcherTimer();
公共主窗口()
{
初始化组件();
DataContext=this;
MyCollection=新的ObservableCollection();
_MyCollection=新列表();
Dispatchermer.Interval=新的时间跨度(0,0,0,0,500);
dispatchermer.Tick+=新事件处理程序(dispatchermer\u Tick);
线程t=新线程(新线程开始(()=>
{
对于(int i=0;i<10;i++)
{
_添加(新的MyClass()
{
整数=i,
Str=“String”+i
});
睡眠(500);
}
}));
t、 Start();
dispatchermer.Start();
}
私有void Dispatcher_Tick(对象发送方,事件参数e)
{
如果(_MyCollection.Count!=MyCollection.Count)
{
添加(_MyCollection[_MyCollection.Count-1]);
}
}
}
第二个编辑示例:

namespace WpfApplication1
{
    public class MyClass
    {
        public int Integer { get; set; }
        public string Str { get; set; }
    }

    public partial class MainWindow : Window
    {
        private ObservableCollection<MyClass> _MyCollection;
        public ObservableCollection<MyClass> MyCollection { get; set; }

        public MainWindow()
        {
            InitializeComponent();
            DataContext = this;
            MyCollection = new ObservableCollection<MyClass>();
            _MyCollection = new ObservableCollection<MyClass>();
            _MyCollection.CollectionChanged += new NotifyCollectionChangedEventHandler(_MyCollection_CollectionChanged);

            Thread t = new Thread(new ThreadStart(() =>
            {
                for (int i = 0; i < 10; i++)
                {
                    _MyCollection.Add(new MyClass()
                    {
                        Integer = i,
                        Str = "String " + i
                    });
                    Thread.Sleep(500);
                }
            }));

            t.Start();
        }

        private void _MyCollection_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            Dispatcher.Invoke(new Action(
                () =>
                {
                    foreach (var item in e.NewItems)
                        MyCollection.Add((MyClass)item);
                }));
        }
    }
}
命名空间WpfApplication1
{
公共类MyClass
{
公共整数{get;set;}
公共字符串Str{get;set;}
}
公共部分类主窗口:窗口
{
私人可观察收集(MyCollection);;
公共ObservableCollection MyCollection{get;set;}
公共主窗口()
{
初始化组件();
DataContext=this;
MyCollection=新的ObservableCollection();
_MyCollection=新的ObservableCollection();
_MyCollection.CollectionChanged+=新的NotifyCollectionChangedEventHandler(\u MyCollection\u CollectionChanged);
线程t=新线程(新线程开始(()=>
{
public partial class MainWindow : Window
    {
        private List<MyClass> _MyCollection;
        public ObservableCollection<MyClass> MyCollection { get; set; }

        private DispatcherTimer dispatcherTimer = new DispatcherTimer();

        public MainWindow()
        {
            InitializeComponent();
            DataContext = this;
            MyCollection = new ObservableCollection<MyClass>();
            _MyCollection = new List<MyClass>();
            dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 500);
            dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);

            Thread t = new Thread(new ThreadStart(() =>
            {
                for (int i = 0; i < 10; i++)
                {
                    _MyCollection.Add(new MyClass()
                    {
                        Integer = i,
                        Str = "String " + i
                    });
                    Thread.Sleep(500);
                }
            }));

            t.Start();
            dispatcherTimer.Start();
        }

        private void dispatcherTimer_Tick(object sender, EventArgs e)
        {
            if (_MyCollection.Count != MyCollection.Count)
            {
                MyCollection.Add(_MyCollection[_MyCollection.Count - 1]);
            }
        }
    }
namespace WpfApplication1
{
    public class MyClass
    {
        public int Integer { get; set; }
        public string Str { get; set; }
    }

    public partial class MainWindow : Window
    {
        private ObservableCollection<MyClass> _MyCollection;
        public ObservableCollection<MyClass> MyCollection { get; set; }

        public MainWindow()
        {
            InitializeComponent();
            DataContext = this;
            MyCollection = new ObservableCollection<MyClass>();
            _MyCollection = new ObservableCollection<MyClass>();
            _MyCollection.CollectionChanged += new NotifyCollectionChangedEventHandler(_MyCollection_CollectionChanged);

            Thread t = new Thread(new ThreadStart(() =>
            {
                for (int i = 0; i < 10; i++)
                {
                    _MyCollection.Add(new MyClass()
                    {
                        Integer = i,
                        Str = "String " + i
                    });
                    Thread.Sleep(500);
                }
            }));

            t.Start();
        }

        private void _MyCollection_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            Dispatcher.Invoke(new Action(
                () =>
                {
                    foreach (var item in e.NewItems)
                        MyCollection.Add((MyClass)item);
                }));
        }
    }
}