WPF界面開(kāi)發(fā)工具DevExpress WPF使用教程:創(chuàng)建綁定到數(shù)據(jù)的3D圖表控件(第一部分)
下載DevExpress v20.1完整版 DevExpress v20.1漢化資源獲取
通過(guò)DevExpress WPF Controls,您能創(chuàng)建有著強(qiáng)大互動(dòng)功能的XAML基礎(chǔ)應(yīng)用程序,這些應(yīng)用程序?qū)W⒂诋?dāng)代客戶(hù)的需求和構(gòu)建未來(lái)新一代支持觸摸的解決方案。
在本教程中,您將完成可視化數(shù)據(jù)源所需的步驟。
應(yīng)該執(zhí)行以下步驟,本文我們將為大家介紹3個(gè)步驟及最后結(jié)果,更多完整內(nèi)容歡迎持續(xù)關(guān)注!
- Step 1. 編寫(xiě)一個(gè)應(yīng)用程序
- Step 2. 為圖表和系列綁定添加數(shù)據(jù)
- Step 3. 配置系列視圖
- 結(jié)果
Step 1. 編寫(xiě)一個(gè)應(yīng)用程序
您將帶有Model和ViewModel類(lèi)的數(shù)據(jù)文件添加到項(xiàng)目中。
- 運(yùn)行MS Visual Studio 2010、2012、2013或2015。
- 創(chuàng)建一個(gè)全新的WPF Application項(xiàng)目
- 添加一個(gè)新的模型類(lèi)。 為此,在解決方案資源管理器中右鍵單擊該項(xiàng)目。從invoked菜單中,選擇Add | New Item... element。
在調(diào)用的Add New Item對(duì)話框中,選擇Code組,然后從項(xiàng)目列表中選擇Class,將文件名設(shè)置為Star.cs,然后單擊OK。
- 將添加的文件包含的代碼替換為以下代碼,該代碼描述此入門(mén)課程中使用的模型對(duì)象。
C#
namespace GettingStarted2 { public struct Star { public int HipID { get; private set; } public string Spectr { get; private set; } public double Luminocity { get; private set; } public double ColorIndex { get; private set; } public double X { get; private set; } public double Y { get; private set; } public double Z { get; private set; } public Star( int id, double x, double y, double z, string spectr, double luminocity, double colorIndex ) { HipID = id; X = x; Y = y; Z = z; Spectr = spectr; Luminocity = luminocity; ColorIndex = colorIndex; } } }
VB.NET
Public Class Star Dim mHipID As Int32 Dim mSpectr As String Dim mX, mY, mZ, mLuminocity, mColorIndex As Double Public ReadOnly Property HipID() As Int32 Get Return mHipID End Get End Property Public ReadOnly Property Spectr() As String Get Return mSpectr End Get End Property Public ReadOnly Property X() As Double Get Return mX End Get End Property Public ReadOnly Property Y() As Double Get Return mY End Get End Property Public ReadOnly Property Z() As Double Get Return mZ End Get End Property Public ReadOnly Property Luminocity() As Double Get Return mLuminocity End Get End Property Public ReadOnly Property ColorIndex() As Double Get Return mColorIndex End Get End Property Public Sub New( ByVal id As Int32, ByVal x As Double, ByVal y As Double, ByVal z As Double, ByVal spectr As String, ByVal luminocity As Double, ByVal colorIndex As Double) mHipID = id mX = x mY = y mZ = z mSpectr = spectr mLuminocity = luminocity mColorIndex = colorIndex End Sub End Class
- 將數(shù)據(jù)文件添加到項(xiàng)目中。 將DevExpress Charts Demo隨附的stardata.csv文件復(fù)制到項(xiàng)目目錄新創(chuàng)建的Data目錄中。
注意:默認(rèn)情況下,該文件位于C:\Users\Public\Documents\DevExpress Demos 20.1\Components\WPF\CS\ChartsDemo.Wpf\Data目錄中。
在解決方案資源管理器中,切換Show All Files按鈕,然后右鍵單擊Data目錄。從調(diào)用的菜單中,選擇Include In Project。
單擊解決方案資源管理器中的stardata.csv文件,然后在Properties窗口中,將Build Action屬性設(shè)置為Resource。
- ViewModel應(yīng)該從數(shù)據(jù)文件加載模型對(duì)象,像以前一樣,將文件添加到ViewModel的項(xiàng)目中。
用以下代碼替換新文件中的代碼。
C#
using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Globalization; using System.IO; using System.Windows; using System.Windows.Resources; namespace GettingStarted2 { public class StarStatisticsViewModel { public IEnumerable<Star> Stars { get; } public StarStatisticsViewModel() { Stars = StarStatisticsLoader.Load("/Data/starsdata.csv"); } } static class StarStatisticsLoader { public static IEnumerable<Star> Load(string filepath) { StreamResourceInfo streamInfo = Application.GetResourceStream( new Uri(filepath, UriKind.RelativeOrAbsolute) ); StreamReader reader = new StreamReader(streamInfo.Stream); Collection<Star> stars = new Collection<Star>(); while (!reader.EndOfStream) { String dataLine = reader.ReadLine(); String[] serializedValues = dataLine.Split(';'); stars.Add( new Star( id: Convert.ToInt32(serializedValues[0], CultureInfo.InvariantCulture), x: Convert.ToDouble(serializedValues[3], CultureInfo.InvariantCulture), y: Convert.ToDouble(serializedValues[4], CultureInfo.InvariantCulture), z: Convert.ToDouble(serializedValues[5], CultureInfo.InvariantCulture), spectr: serializedValues[1], luminocity: Convert.ToDouble(serializedValues[6], CultureInfo.InvariantCulture), colorIndex: Convert.ToDouble(serializedValues[2], CultureInfo.InvariantCulture) ) ); } return stars; } } }
VB.NET
Imports System.Collections.ObjectModel Imports System.Globalization Imports System.IO Imports System.Windows.Resources Public Class StarDataViewModel Dim mStars As IEnumerable(Of Star) Public ReadOnly Property Stars As IEnumerable(Of Star) Get Return mStars End Get End Property Public Sub New() mStars = StarStatisticsLoader.Load("/Data/starsdata.csv") End Sub End Class Public Module StarStatisticsLoader Public Function Load(ByVal filepath As String) As IEnumerable(Of Star) Dim streamInfo As StreamResourceInfo = Application.GetResourceStream( New Uri(filepath, UriKind.RelativeOrAbsolute) ) Dim reader As StreamReader = New StreamReader(streamInfo.Stream) Dim stars As Collection(Of Star) = New Collection(Of Star)() While (Not reader.EndOfStream) Dim dataLine As String = reader.ReadLine() Dim serializedValues As String() = dataLine.Split(";") stars.Add( New Star( id:=Convert.ToInt32(serializedValues(0), CultureInfo.InvariantCulture), x:=Convert.ToDouble(serializedValues(3), CultureInfo.InvariantCulture), y:=Convert.ToDouble(serializedValues(4), CultureInfo.InvariantCulture), z:=Convert.ToDouble(serializedValues(5), CultureInfo.InvariantCulture), spectr:=serializedValues(1), luminocity:=Convert.ToDouble(serializedValues(6), CultureInfo.InvariantCulture), colorIndex:=Convert.ToDouble(serializedValues(2), CultureInfo.InvariantCulture) ) ) End While Return stars End Function End Module
- 現(xiàn)在,將ViewModel分配給Window.DataContext屬性:選擇Window,找到DataContext屬性,然后單擊New按鈕。在調(diào)用的窗口中,選擇GettingStarted.StarDataViewModel類(lèi),然后單擊OK。
準(zhǔn)備工作完成,這下一篇文章中將詳細(xì)說(shuō)明如何添加Chart3D控件,分配數(shù)據(jù)和自定義顯示設(shè)置。
DevExpress技術(shù)交流群2:775869749 歡迎一起進(jìn)群討論
富文本控件難上手?這堂公開(kāi)課你一定不能錯(cuò)過(guò),不同視圖不同應(yīng)用場(chǎng)景全解!