下面是关于如何创建一个基本麻雀图的这教程。基本上包括创建一个ViewModel类和在图表的DataContext中设置ViewModel。
The ViewModel:
//Create a model
public class Model
{
public double X { get; set; }
public double Y { get; set; }
public Model(double x,double y)
{
X = x;
Y = y;
}
}
// Create a ViewModel
public class ViewModel
{
public ObservableCollection<Model> Collection { get; set; }
public ViewModel()
{
Collection = new ObservableCollection<Model>();
GenerateDatas();
}
private void GenerateDatas()
{
this.Collection.Add(new Model(0, 1));
this.Collection.Add(new Model(1, 2));
this.Collection.Add(new Model(2, 3));
this.Collection.Add(new Model(3, 4));
}
}XAML:
//Use the viewmodel in the Sparrow Chart
<sparrow:SparrowChart>
<sparrow:SparrowChart.DataContext>
**<local:ViewModel/>**
</sparrow:SparrowChart.DataContext>
<sparrow:SparrowChart.XAxis>
<sparrow:LinearXAxis/>
</sparrow:SparrowChart.XAxis>
<sparrow:SparrowChart.YAxis>
<sparrow:LinearYAxis/>
</sparrow:SparrowChart.YAxis>
<sparrow:LineSeries PointsSource="{Binding Collection}" XPath="X" YPath="Y"/>
如何定义本地:命名空间,然后在其中包含ViewModel,以便它在DataContext中运行良好?
发布于 2015-03-05 16:05:58
将下面一行添加到窗口的XAML代码xmlns:local="clr-namespace:Here.Comes.Your.Namespace"中
在我看来
<Window x:Class="MyProgram.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sparrow="http://sparrowtoolkit.codeplex.com/wpf"
xmlns:local="clr-namespace:MyProgram"
Title="My Program" Height="306" Width="736" MinWidth="680" MinHeight="440">另外,要将用于将数据提供给SparrowChart的集合进行实际绑定,请使用ViewModel实例设置图表的DataContext。
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public sealed partial class MainWindow
{
ViewModel myViewModel;
public MainWindow()
{
...
myViewModel = new ViewModel();
mySparrowChart.DataContext = myViewModel;
}
}当然,斯帕罗图的控制名是mySparrowChart ( <sparrow:SparrowChart Name="mySparrowChart">)。
https://stackoverflow.com/questions/28250056
复制相似问题