界面控件DevExpress WPF入门级教程:数据源 – 实时源

本文主要介绍DevExpress WPF数据源中的实时源类型,欢迎下载最新版体验!

RealTimeSource 组件旨在显示少量快速变化的数据,同时保持用户界面响应。

注意:RealTimeSource可以有效地处理不超过数百条数据记录的数据源。

下图演示了使用RealTimeSource绑定到动态数据的GridControl。

界面控件DevExpress WPF入门级教程:格式化值

如果您的数据源中的记录经常更新(例如,每秒 20 000 次更新),可以考虑使用RealTimeSource 组件。

将RealTimeSource与静态数据一起使用不会导致任何性能改进。

注意:RealTimeSource是数据感知控件和实际数据之间的一层。

在决定在应用程序中使用 RealTimeSource 组件之前,请考虑以下限制。

  • RealTimeSource 可以有效地处理不超过数百条数据记录的数据源。
  • 当 GridControl 使用 RealTimeSource 绑定到数据时,不支持就地编辑(以及通过内置编辑表单进行编辑)。
  • RealTimeSource 不跟踪数据更改,要显示数据更新,请确保您的数据源提供更改通知。

支持以下通知类型。

  • IBindingList.ListChanged
  • INotifyCollectionChanged.CollectionChanged
  • INotifyPropertyChanged.PropertyChanged
初始化实时源

您可以将任何数据感知控件绑定到 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 namesmyRealTimeSource.DisplayableProperties = "Id;Progress";

场景:忽略对单个属性值的修改。

方法:将RealTimeSource.IgnoreItemEvents属性设置为 true。RealTimeSource将忽略对数据源项的修改,只有对数据源列表所做的修改才会传递给数据感知控件。

示例

此示例演示如何使用 RealTimeSource 组件在 GridControl 中显示快速变化的数据。

注意:此示例中的数据是出于演示目的随机生成的。

MainWindow.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));}}}

MainWindow.xaml.vb

Imports Microsoft.VisualBasicImports SystemImports System.Collections.GenericImports System.LinqImports System.WindowsImports System.Collections.ObjectModelImports System.GlobalizationImports System.Windows.DataImports System.Windows.MarkupImports DevExpress.DataImports System.Windows.ThreadingImports System.ComponentModelNamespace RealTimeSourceExamplePartial Public Class MainWindowInherits WindowPrivate Persons As ObservableCollection(Of Data)Private Count As Integer = 50Private Random As New Random()Public Sub New()InitializeComponent()Persons = New ObservableCollection(Of Data)()For i As Integer = 0 To Count - 1Persons.Add(New Data With {.Id = i, .Text = "Text" & i, .Progress = GetNumber()})Next igrid.ItemsSource = New RealTimeSource() With {.DataSource = Persons}Dim timer As New DispatcherTimer()timer.Interval = TimeSpan.FromMilliseconds(1)AddHandler timer.Tick, AddressOf Ticktimer.Start()End SubPrivate 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 SubPrivate Function GetNumber() As IntegerReturn Random.Next(0, Count)End FunctionEnd ClassPublic Class DataImplements INotifyPropertyChangedPrivate _Id As IntegerPublic _Text As StringPublic _Progress As DoublePublic Property Id() As IntegerGetReturn _IdEnd GetSet(ByVal value As Integer)_Id = valueNotifyPropertyChanged("Id")End SetEnd PropertyPublic Property Text() As StringGetReturn _TextEnd GetSet(ByVal value As String)_Text = valueNotifyPropertyChanged("Text")End SetEnd PropertyPublic Property Progress() As DoubleGetReturn _ProgressEnd GetSet(ByVal value As Double)_Progress = valueNotifyPropertyChanged("Progress")End SetEnd PropertyPublic Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChangedPrivate Sub NotifyPropertyChanged(ByVal name As String)RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(name))End SubEnd ClassEnd Namespace

MainWindow.xaml

<Windowx:Class="RealTimeSourceExample.MainWindow"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors"xmlns:dx="http://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>

DevExpress WPF | 下载试用

DevExpress WPF拥有120+个控件和库,将帮助您交付满足甚至超出企业需求的高性能业务应用程序。通过DevExpress WPF能创建有着强大互动功能的XAML基础应用程序,这些应用程序专注于当代客户的需求和构建未来新一代支持触摸的解决方案。 无论是Office办公软件的衍伸产品,还是以数据为中心的商业智能产品,都能通过DevExpress WPF控件来实现。


DevExpress技术交流群5:742234706      欢迎一起进群讨论

DevExpress线上公开课主题票选火热开启,主题由你来定!点击填写问卷

DevExpress企业定制服务
标签:

来源:慧都

声明:本站部分文章及图片转载于互联网,内容版权归原作者所有,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2022年1月11日
下一篇 2022年1月11日

相关推荐

发表回复

登录后才能评论