使用ifd语句将数据加载到silverlight线图中时出现问题

时间:2011-03-16 18:02:11

标签: c# silverlight silverlight-4.0

我现在已经玩了两天Silverlight并且正在取得进展但是已经遇到障碍了。

我根本没有C#经验(我是PHP程序员)。

我有一个显示数据没有问题的折线图,但我想显示不同的数据,具体取决于传递的信息。这一切都已经完成,但我已经退后一步尝试获得基本的if / else工作

我的xaml.cs如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace MyProject
{
public partial class MainPage : UserControl
{
    public MainPage()
    {
        InitializeComponent();
        this.Loaded += new RoutedEventHandler(UC_Loaded);
    }

    void UC_Loaded(object sender, RoutedEventArgs e)
    {
        //int student_id = (int)App.Current.Resources["student_id"];
        int student_id = (int)10;
        //int test_id = (int)App.Current.Resources["test_id"];

        this.DataContext = this;
        List<Student> cust = new List<Student>();

        if (student_id==10)
        {

            cust.Add(new Student() { Date = "14th Oct", Result = 30 });
            cust.Add(new Student() { Date = "20th Oct", Result = 60 });
            cust.Add(new Student() { Date = "30th Oct", Result = 20 });
            cust.Add(new Student() { Date = "12th Nov", Result = 10 });
            cust.Add(new Student() { Date = "20th Nov", Result = 70 });

        }
        else
        {

            cust.Add(new Student() { Date = "14th Oct", Result = 10 });
            cust.Add(new Student() { Date = "20th Oct", Result = 10 });
            cust.Add(new Student() { Date = "30th Oct", Result = 10 });
            cust.Add(new Student() { Date = "12th Nov", Result = 10 });
            cust.Add(new Student() { Date = "20th Nov", Result = 10 });

        }
        this.DataContext = cust;
    }
}

public class Student
{
    public string Date { get; set; }
    public int Result { get; set; }
}
}

我的XAML在这里:

<Grid x:Name="LayoutRoot" Background="White">
    <StackPanel>
        <toolkit:Chart Height="500" Width="600" Title="Test title">
            <toolkit:Chart.Series>
                <toolkit:LineSeries Title="Student Scores"
                                    ItemsSource="{Binding}"
                                    IndependentValueBinding="{Binding Date}"
                                    DependentValueBinding="{Binding Result}">
                    <toolkit:LineSeries.DataPointStyle>
                        <Style TargetType="toolkit:LineDataPoint">
                            <Setter Property="Background" Value="Lime"/>
                        </Style>
                    </toolkit:LineSeries.DataPointStyle>
                 </toolkit:LineSeries>
            </toolkit:Chart.Series>
            <toolkit:Chart.Axes>
                <toolkit:LinearAxis Orientation="Y" Minimum="0" Maximum="100" Interval="5" ShowGridLines="True" FontStyle="Italic"></toolkit:LinearAxis>
            </toolkit:Chart.Axes>

        </toolkit:Chart>
    </StackPanel>
</Grid>

现在如果我运行它而没有if / else和一组cust.Add它按预期工作但是使用if / else我得到一个没有点的空图。

提前致谢,希望这是有道理的!

2 个答案:

答案 0 :(得分:1)

只是为了澄清一下,摩西给出的答案是那里还是有一些调整:

不要忘记添加:

using System.ComponentModel; 

并改变:

public class Student : INotifyLayoutChange

public class Student : INotifyPropertyChanged

答案 1 :(得分:0)

啊,是啊,你可能想要一些关于Binding和INotifyPropertyChanged的教程,这就是说(注意这是来自内存):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;

namespace MyProject
{
    public partial class MainPage : UserControl
    {
        private ObservableCollection<Student> m_Students = new ObservableCollection<Student>();

        public MainPage()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(UC_Loaded);
        }

        public ObservableCollection<Student> Students
        {
            get { return m_Students; }
            set { m_Students = value; }
        }

        void UC_Loaded(object sender, RoutedEventArgs e)
        {
            //int student_id = (int)App.Current.Resources["student_id"];
            int student_id = 10; // no need to type cast.
            //int test_id = (int)App.Current.Resources["test_id"];

            this.DataContext = Students;

            if (student_id == 10)
            {
                Students.Add(new Student() { Date = "14th Oct", Result = 30 });
                Students.Add(new Student() { Date = "20th Oct", Result = 60 });
                Students.Add(new Student() { Date = "30th Oct", Result = 20 });
                Students.Add(new Student() { Date = "12th Nov", Result = 10 });
                Students.Add(new Student() { Date = "20th Nov", Result = 70 });
            }
            else
            {
                Students.Add(new Student() { Date = "14th Oct", Result = 10 });
                Students.Add(new Student() { Date = "20th Oct", Result = 10 });
                Students.Add(new Student() { Date = "30th Oct", Result = 10 });
                Students.Add(new Student() { Date = "12th Nov", Result = 10 });
                Students.Add(new Student() { Date = "20th Nov", Result = 10 });
            }
        }
    }

    public class Student : INotifyLayoutChange
    {
        private string m_Date;
        private int m_Result;

        public event PropertyChangedEventHandler PropertyChanged;

        public string Date 
        { 
            get { return m_Date; } 
            set
            {
                if (m_Date == value)
                    return; // return values are the same, no update needed.
                m_Date = value;
                RaisePropertyChanged("Date");
            }
        }

        public int Result 
        { 
            get { return m_Result; }
            set
            {
                if (m_Result == value)
                    return;
                m_Result = value;
                RaisePropertyChanged("Result");
            }
        }

        private void RaisePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}