I am working on a WPF application. This app uses OxyPlot chart control . I let the user do the calculation. The user can view the result both in the form of a grid, and in the form of a diagram. In an attempt to do this, I have the following:
home.xaml
<DataGrid x:Name="resultDataGrid" ItemsSource="{Binding Items}">
<DataGrid.Columns>
<DataGridTextColumn Header="#" Binding="{Binding Number}" />
<DataGridTextColumn Header="Description" Binding="{Binding Description}" />
</DataGrid.Columns>
</DataGrid>
<oxy:Plot x:Name="resultGraph" Background="Transparent" Height="400" HorizontalContentAlignment="Stretch" VerticalAlignment="Stretch" Visibility="Collapsed">
<oxy:Plot.Series>
<oxy:LineSeries ItemsSource="{Binding Points}" MarkerType="Circle" Title="Result" />
</oxy:Plot.Series>
</oxy:Plot>
<StackPanel Orientation="Horizontal">
<Button x:Name="executeButton" Click="executeButton_Click" />
<Button x:Name="viewData" Click="viewData_Click" />
<Button x:Name="viewChart" Click="viewChart_Click" />
</StackPanel>
home.xaml.cs
public partial class Home: Page
{
private MyViewModel viewModel = null;
public Home()
{
InitializeComponent();
viewModel = new MyViewModel();
this.DataContext = viewModel;
}
private void executeButton_Click(object sender, RoutedEventArgs e) {
viewModel.Execute();
}
private void viewData_Click(object sender, RoutedEventArgs e)
{
resultDataGrid.Visibility = System.Windows.Visibility.Visible;
resultGraph.Visibility = System.Windows.Visibility.Collapsed;
}
private void viewChart_Click(object sender, RoutedEventArgs e)
{
resultDataGrid.Visibility = System.Windows.Visibility.Collapsed;
resultGraph.Visibility = System.Windows.Visibility.Visible;
}
}
MyViewModel.cs
public class MyViewModel : INotifyPropertyChanged
{
public Dictionary<int, string> Items{ get; set; }
public IList<DataPoint> Points{ get; set; }
public void Execute()
{
this.Items = new Dictionary<int, string>();
this.Points = new List<DataPoint>();
var randomNumber = GetRandomNumber();
for (var i=0; i<10; i++)
{
this.Items.Add((i+1), "Item #" + i);
var dataPoint = new DataPoint(i, (randomNumber*i));
this.Points.Add(dataPoint);
}
NotifyPropertyChanged("Items");
NotifyPropertyChanged("Points");
}
}
, "", DataGrid, . , , . , "", DataGrid, . , resultGraph, "", , . , . .