MVVM pattern for WPF - 2d graph without using any toolkits?

The easiest way to create a poor-man's chart in WPF using the MVVM pattern is to transform the data into a format that is easily consumable by markup, specifically segments instead of points.

Up vote 1 down vote favorite share g+ share fb share tw.

Need to draw 2D graph the most easy way (I think - it's polyline) using MVVM pattern in WPF. I've created several classes: namespace SomeNamespace. Models { class Info { // public Queue Dots { get; set; }?

Public int Point { get; set; } public int GetLoad() { return new Random (100); //Get some data from external class } } } namespace SomeNamespace. ViewModels { public abstract class ViewModelBase : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(string propertyName) { PropertyChangedEventHandler handler = PropertyChanged; if (handler! = null) { handler(this, new PropertyChangedEventArgs(propertyName)); } } } class InfoViewModel: ViewModelBase { //private Queue _dots = new Queue(); //public Queue Dots //{ // get { return _dots; } // set // { // _dots = value; // OnPropertyChanged("Dots"); // } //} private int _point; public int Point { get { return _point; } set { _point = value; OnPropertyChanged("Point"); } } } class MainViewModel : ViewModelBase { // public ObservableCollection InfoList { get; set; }?

Public ObservableCollection Points { get; set; } public MainViewModel(List info) { //InfoList = new ObservableCollection(info. Select I => new InfoViewModel( i)));? Points = new ObservableCollection() { 10, 20, 30, 40 }; //just for test } } } In the App.

Xaml public partial class App : Application { private void OnStartup(object sender, StartupEventArgs e) { List I = new List() { new Info(){ Point = 10 }, new Info(){ Point = 15 }, new Info(){ Point = 20 }, new Info(){ Point = 25 }, new Info(){ Point = 30 }, new Info(){ Point = 35 } }; MainWindow mainView = new MainWindow(); MainViewModel mainViewModel = new MainViewModel( i); mainView. DataContext = mainViewModel; mainView.Show(); } } In MainWindow. Xaml But it doesn't work.

EDIT: I've wrote the following code, but I don't understand: 1) How do I bind to Queue? 2)How can the refresh itself every second? Or how can the ViewModel refresh itself every second and notifies the View about it?

Public class Segment { public Queue Dots { get; set; } } public class ViewModel:INotifyPropertyChanged { private Queue _segments; public Queue Segments { get { return _segments; } set { _segments = value; OnPropertyChanged("Segments"); } } public ViewModel(Queue segments) { } public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(string propertyName) { PropertyChangedEventHandler handler = PropertyChanged; if (handler! = null) { handler(this, new PropertyChangedEventArgs(propertyName)); } } } MainWindow mainView = new MainWindow(); Queue q = Class1.GenerateData(); //Class1.GenerateData() returns Queue mainView. DataContext = new ViewModel(q); c# wpf xaml data-binding mvvm link|improve this question edited Dec 10 '11 at 12:51thkala16.3k42039 asked Feb 19 '11 at 17:22Grienders15239 54% accept rate.

The easiest way to create a poor-man's chart in WPF using the MVVM pattern is to transform the data into a format that is easily consumable by markup, specifically segments instead of points. Here is code representative of the view model: public class Segment { public Point From { get; set; } public Point To { get; set; } } public class ViewModel { public IList Segments { get; set; } } void SetDataContext() { var Points = new Point { new Point { X = 0, Y = 10 }, new Point { X = 10, Y = 30 }, new Point { X = 20, Y = 20 }, }; DataContext = new ViewModel { Segments = new List(Points. Zip(Points.

Skip(1), (a, b) => new Segment { From = a, To = be })) }; } and here is how to create a bare-bones chart from that data: which results in this "chart.

It looks like what I looking for! – Grienders Feb 19 '11 at 18:54 Yeah. It's very good.

But I also need to refresh a graph every second to simulate CPU load. Probably I must to implement the interface INotifyPropertyChanged and use Queue. – Grienders Feb 19 '11 at 19:10 Yes, standard MVVM practices apply.

In this case all you need is for ViewModel to implement INotifyPropertyChanged specifically for Segments. – Rick Sladkey Feb 19 '11 at 19:20.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions