实时源
RealTimeSource组件旨在显示少量快速变化的数据,同时保持用户界面的响应性。
提示:RealTimeSource可以有效地处理不超过几百条数据记录的数据源。
下面的动画演示了使用RealTimeSource绑定到动态数据的GridControl。
如果数据源中的记录经常更新(例如,每秒更新20,000次),您应该考虑使用RealTimeSource组件。
使用带有静态数据的RealTimeSource不会带来任何性能改进。
提示:RealTimeSource是数据感知控件和实际数据之间的一层。
在决定在应用程序中使用RealTimeSource组件之前,请考虑以下限制。
- RealTimeSource可以有效地处理不超过几百条数据记录的数据源。
- TreeListView不支持RealTimeSource。
- 当GridControl使用RealTimeSource绑定到数据时,不支持就地编辑(以及通过内置编辑表单进行编辑)。
- RealTimeSource不跟踪数据更改,要显示数据更新,请确保数据源提供更改通知。
支持以下通知类型:
- IBindingList. ListChanged
- INotifyCollectionChanged. CollectionChanged
- INotifyPropertyChanged. PropertyChanged
初始化RealTimeSource
您可以将任何数据感知控件绑定到RealTimeSource,以GridControl为例进行说明。
要在GridControl中显示实时数据,请执行以下操作:
- 在代码中,创建RealTimeSource对象,使用它的RealTimeSource.DataSource属性将一个实时源链接到您的数据。
- 设置LookUpEditBase.ItemsSource属性(通过GridControl.ItemsSource)到RealTimeSource对象。
C#:
ObservableCollection<Data> Persons; //... DevExpress.Data.RealTimeSource myRealTimeSource = new DevExpress.Data.RealTimeSource(); myRealTimeSource.DataSource = Persons; myGridControl.ItemsSource = myRealTimeSource;
点击复制
下表描述了其他RealTimeSource配置场景。
- 指定将哪些数据源属性传递给数据感知控件。
使用RealTimeSource.DisplayableProperties属性指定所需的数据源属性。
C#:
//A semicolon-separated list of data source property names myRealTimeSource.DisplayableProperties = "Id;Progress";
点击复制
- 忽略单个属性值的修改。
设置RealTimeSource. IgnoreItemEvents属性设置为true。
对数据源项的修改将被RealTimeSource忽略。
只有对数据源列表所做的修改才会传递给数据感知控件。
示例
这个例子演示了如何使用RealTimeSource组件在GridControl中显示快速变化的数据。
提示:本例中的数据是为了演示而随机生成的。
MainWindows.xaml.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Windows; using System.Collections.ObjectModel; using System.Globalization; using System.Windows.Data; using System.Windows.Markup; using DevExpress.Data; using System.Windows.Threading; using System.ComponentModel; namespace RealTimeSourceExample { public partial class MainWindow: Window { ObservableCollection<Data> Persons; int Count = 50; Random Random = new Random(); public MainWindow() { InitializeComponent(); Persons = new ObservableCollection<Data>(); for (int i = 0; i < Count; i++) Persons.Add(new Data { Id = i, Text = "Text" + i, Progress = GetNumber() }); grid.ItemsSource = new RealTimeSource() { DataSource = Persons }; DispatcherTimer timer = new DispatcherTimer(); timer.Interval = TimeSpan.FromMilliseconds(1); timer.Tick += Tick; timer.Start(); } private void Tick(object sender, EventArgs e) { int index = Random.Next(0, Count); Persons[index].Id = GetNumber(); Persons[index].Text = "Text" + GetNumber(); Persons[index].Progress = GetNumber(); } int GetNumber() { return Random.Next(0, Count); } } public class Data: INotifyPropertyChanged { private int _Id; public string _Text; public double _Progress; public int Id { get { return _Id; } set { _Id = value; NotifyPropertyChanged("Id"); } } public string Text { get { return _Text; } set { _Text = value; NotifyPropertyChanged("Text"); } } public double Progress { get { return _Progress; } set { _Progress = value; NotifyPropertyChanged("Progress"); } } public event PropertyChangedEventHandler PropertyChanged; void NotifyPropertyChanged(string name) { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(name)); } } }
点击复制
MainWindows.xaml.vb:
Imports Microsoft.VisualBasic Imports System Imports System.Collections.Generic Imports System.Linq Imports System.Windows Imports System.Collections.ObjectModel Imports System.Globalization Imports System.Windows.Data Imports System.Windows.Markup Imports DevExpress.Data Imports System.Windows.Threading Imports System.ComponentModel Namespace RealTimeSourceExample Partial Public Class MainWindow Inherits Window Private Persons As ObservableCollection(Of Data) Private Count As Integer = 50 Private Random As New Random() Public Sub New() InitializeComponent() Persons = New ObservableCollection(Of Data)() For i As Integer = 0 To Count - 1 Persons.Add(New Data With {.Id = i, .Text = "Text" & i, .Progress = GetNumber()}) Next i grid.ItemsSource = New RealTimeSource() With {.DataSource = Persons} Dim timer As New DispatcherTimer() timer.Interval = TimeSpan.FromMilliseconds(1) AddHandler timer.Tick, AddressOf Tick timer.Start() End Sub Private Sub Tick(ByVal sender As Object, ByVal e As EventArgs) Dim index As Integer = Random.Next(0, Count) Persons(index).Id = GetNumber() Persons(index).Text = "Text" & GetNumber() Persons(index).Progress = GetNumber() End Sub Private Function GetNumber() As Integer Return Random.Next(0, Count) End Function End Class Public Class Data Implements INotifyPropertyChanged Private _Id As Integer Public _Text As String Public _Progress As Double Public Property Id() As Integer Get Return _Id End Get Set(ByVal value As Integer) _Id = value NotifyPropertyChanged("Id") End Set End Property Public Property Text() As String Get Return _Text End Get Set(ByVal value As String) _Text = value NotifyPropertyChanged("Text") End Set End Property Public Property Progress() As Double Get Return _Progress End Get Set(ByVal value As Double) _Progress = value NotifyPropertyChanged("Progress") End Set End Property Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged Private Sub NotifyPropertyChanged(ByVal name As String) RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(name)) End Sub End Class End Namespace
点击复制
MainWindows.xaml:
<Window x:Class="RealTimeSourceExample.MainWindow" xmlns="//schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="//schemas.microsoft.com/winfx/2006/xaml" xmlns:dxg="//schemas.devexpress.com/winfx/2008/xaml/grid" xmlns:dxe="//schemas.devexpress.com/winfx/2008/xaml/editors" xmlns:dx="//schemas.devexpress.com/winfx/2008/xaml/core" xmlns:local="clr-namespace:RealTimeSourceExample" Name="win" Title="MainWindow" Height="350" Width="525"> <Grid> <dxg:GridControl x:Name="grid"> <dxg:GridControl.Columns> <dxg:GridColumn FieldName="Id"/> <dxg:GridColumn FieldName="Text"/> <dxg:GridColumn FieldName="Progress"> <dxg:GridColumn.EditSettings> <dxe:ProgressBarEditSettings/> </dxg:GridColumn.EditSettings> </dxg:GridColumn> </dxg:GridControl.Columns> <dxg:GridControl.View> <dxg:TableView Name="view" AutoWidth="True"/> </dxg:GridControl.View> </dxg:GridControl> </Grid> </Window>
点击复制