我需要使用 以最简单的方式(我认为是折线或线)实现 2D 图形WPF 中的 MVVM 模式 .
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));
}
}
}
风景
MainWindow mainView = new MainWindow();
Queue<Point> q = Class1.GenerateData(); //Class1.GenerateData() returns Queue<Point>
mainView.DataContext = new ViewModel(q);
但我不明白
1) 如何绑定(bind)
<Line X1="{Binding ??}" Y1="{Binding ??}" X2="{Binding ??}" Y2="{Binding ??}" Stroke="Red"/>
至Queue < Point >
?2)
< Line .../>
每秒刷新一次?或者 ViewModel 如何每秒刷新一次并通知 View 呢?
最佳答案
在我看来,最简单的方法是使用 WPF Toolkit 的 System.Windows.Controls.DataVisualization.Toolkit.dll 来生成一个非常简单的折线图。我创建了一个示例应用程序,您可以从这里下载 here .它使用 MVVM 模式,但我使用的是 ObservableCollection 点而不是队列。使用 ObservableCollection 将确保您的后端集合更改后, View 中的图表将相应更新。
如果您有任何具体要求,请告诉我。
关于wpf - WPF 的 MVVM 模式 - 再次绘制 2d 图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5056162/