首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从DataContext为麻雀图WP8提供ViewModel

从DataContext为麻雀图WP8提供ViewModel
EN

Stack Overflow用户
提问于 2015-01-31 09:20:31
回答 1查看 576关注 0票数 0

下面是关于如何创建一个基本麻雀图的教程。基本上包括创建一个ViewModel类和在图表的DataContext中设置ViewModel。

The ViewModel:

代码语言:javascript
复制
//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:

代码语言:javascript
复制
//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中运行良好?

EN

回答 1

Stack Overflow用户

发布于 2015-03-05 16:05:58

将下面一行添加到窗口的XAML代码xmlns:local="clr-namespace:Here.Comes.Your.Namespace"

在我看来

代码语言:javascript
复制
<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。

代码语言:javascript
复制
/// <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">)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28250056

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档