-MVVM模式(封裝數(shù)據(jù)綁定類))
知識(shí)點(diǎn)1. 數(shù)據(jù)綁定數(shù)據(jù)綁定類 與 圖形操作在同一個(gè)類中2. X軸上顯示點(diǎn)數(shù)主類 中3. 添加隨機(jī)值在圖表中 值針對(duì)Y軸值4. 清空?qǐng)D表5. 圖標(biāo)titel顯示實(shí)例圖表控件綁定 SeriesCollectionA Utils_ChartA.cs中l(wèi)vc:CartesianChart x:NameLiveChartsA Margin10,146,185,51 Series{Binding SeriesCollectionA} Background#FFF8FCFD LegendLocationTop /lvc:CartesianChart提示框綁定 MsgTips Utils_ChartA.cs中Label Grid.Row2 Grid.Column0 FontSize12 Background#22FF0000 Content{Binding MsgTips} /Utils_ChartA.cs(數(shù)據(jù)綁定類 與 圖形操作在同一個(gè)類中)using System; using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; using System.Runtime.CompilerServices; using System.Text; using System.Windows.Media; using LiveCharts; using LiveCharts.Wpf; namespace Wpf_Math.MyUC_TestChart { public class Utils_ChartA : INotifyPropertyChanged { private string _msgTips; private SeriesCollection _seriesCollectionA; public Utils_ChartA() { // 在構(gòu)造函數(shù)中初始化 _seriesCollectionA new SeriesCollection(); } public string MsgTips { get _msgTips; set { _msgTips value; OnPropertyChanged(); } } public SeriesCollection SeriesCollectionA { get _seriesCollectionA; set { if (_seriesCollectionA ! value) { _seriesCollectionA value; OnPropertyChanged(); } } } public void InitializeLiveCharts1(int XPonitLength) { if (XPonitLength 0) XPonitLength 100; LineSeries myLineseries new LineSeries() //實(shí)例化一條折線圖 { Title M485-----電壓測(cè)試圖, //設(shè)置折線的標(biāo)題 StrokeThickness 1, // 設(shè)置折線粗細(xì) LineSmoothness 1, //折線圖直線形式 0 or 1 PointGeometry null, //折線圖的無(wú)點(diǎn)樣式 Stroke Brushes.Red, // 紅色線條 Values new ChartValuesdouble(new Double[XPonitLength]) //添加折線圖的數(shù)據(jù)(X軸劃分點(diǎn)數(shù)) }; SeriesCollectionA.Add(myLineseries); } public void addData(double n) { //通過Dispatcher在工作線程中更新窗體的UI元素 Debug.Write(n n \r\n); SeriesCollectionA[0].Values.Add(n); SeriesCollectionA[0].Values.RemoveAt(0); } public void Clear(CartesianChart Chart) { Chart.Series.Clear(); //清空數(shù)據(jù) InitializeLiveCharts1(0); //重復(fù)新初始化 } public event PropertyChangedEventHandler? PropertyChanged; protected void OnPropertyChanged([CallerMemberName] string name null) PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name)); } }主操作界面using System; using System.Collections.Generic; using System.Diagnostics; using System.Text; 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; using LiveCharts; using LiveCharts.Wpf; namespace Wpf_Math.MyUC_TestChart { public partial class UC_ChartA : UserControl { int i; Utils_ChartA ChartA new Utils_ChartA(); public UC_ChartA() { InitializeComponent(); this.DataContext ChartA; ChartA.MsgTips 提示; ChartA.InitializeLiveCharts1(0); } private void Button_Click(object sender, RoutedEventArgs e) { Button button (Button)sender; ChartA.MsgTips 你點(diǎn)擊了-- button.Content i; //Debug.Write(你點(diǎn)擊了--btn_calulate\\r\n) switch (button.Name) { case btn_addA: { ChartA.addData(new Random().Next(0, 30)); break; } case btn_addB: { ChartA.addData(new Random().Next(80, 99)); break; } case btn_clear:{ ChartA.Clear(LiveChartsA);break;} default: break; } } } }將持續(xù)更新