Problem loading data into silverlight line graph with ifd statements
I have been playing around with silverlight for a couple of days now and am making progress but have hit a road block.
I have no C# experience at all (I'm a PHP programmer).
I have a line graph displaying data with no problem but I want to show different data depending on what information is being passed. It is all set-up but I have taken a step back to try and get the basic if/else working
My xaml.cs is as follows:
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; }
}
}
My XAML is here:
<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="It开发者_Go百科alic"></toolkit:LinearAxis>
</toolkit:Chart.Axes>
</toolkit:Chart>
</StackPanel>
</Grid>
Now if I run it without the if/else and one set of the cust.Add it works as expected but with the if/else I get an empty graph with no points on it.
Thanks in advance and hope this makes sense!
Just to clarify, the answer given by Moses is there or there abouts with a couple of tweaks:
Don't forget to add:
using System.ComponentModel;
And Change:
public class Student : INotifyLayoutChange
to
public class Student : INotifyPropertyChanged
Ahhhh yeah, you might want to google some tutorials on Binding and INotifyPropertyChanged, that being said (note this was from memory):
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));
}
}
}
精彩评论