开发者

MVVM pattern for WPF - 2d graph again

开发者 https://www.devze.com 2023-02-12 17:54 出处:网络
I ha开发者_StackOverflow中文版ve a need to implement 2D graph the most easy way (I think - it\'s polyline or line) using MVVM pattern in WPF.

I ha开发者_StackOverflow中文版ve a need to implement 2D graph the most easy way (I think - it's polyline or line) using MVVM pattern in WPF.

public class Segment
    {
        public Queue<Point> Dots { get; set; }

    }

    public class ViewModel:INotifyPropertyChanged
    {
        private Queue<Segment> _segments;
        public Queue<Segment> Segments
        {
            get { return _segments; }
            set
            {
                _segments = value;
                OnPropertyChanged("Segments");
            }
        }


        public ViewModel(Queue<Point> segments)
        {

        }


        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;

            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

the view

MainWindow mainView = new MainWindow();
  Queue<Point> q = Class1.GenerateData(); //Class1.GenerateData() returns Queue<Point>  
  mainView.DataContext = new ViewModel(q);

But I don't understand

1) How do I bind <Line X1="{Binding ??}" Y1="{Binding ??}" X2="{Binding ??}" Y2="{Binding ??}" Stroke="Red"/> to Queue < Point > ?

2)How can the < Line .../> refresh itself every second? Or how can the ViewModel refresh itself every second and notifies the View about it?


In my opinion, most easiest way would be to use System.Windows.Controls.DataVisualization.Toolkit.dll of WPF Toolkit to generate a very simple Line graph. I have created a sample application which you download from here here. It uses MVVM pattern but instead of Queue I have used an ObservableCollection of Point. Use of ObservableCollection will make sure that as soon as your back-end collection changes, your chart in the view will be updated accordingly.

Let me know if you have any specific requirements.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号