我不知道如何使用ObservableCollection显示一些点。这是我的代码:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d3="http://research.microsoft.com/DynamicDataDisplay/1.0"
Title="MainWindow" Height="350" Width="525">
<Grid>
<d3:ChartPlotter x:Name="Plotter" Margin="100,5,0,0">
<d3:LineGraph />
</d3:ChartPlotter>
<Button x:Name="button"
Content="Load Graph"
HorizontalAlignment="Left"
Margin="10,35,0,0"
VerticalAlignment="Top"
Width="70"
Height="45" Click="button_Click"/>
</Grid>
public partial class MainWindow : Window
{
public ObservableCollection<Point> Data { get; set; }
public MainWindow()
{
InitializeComponent();
Data = new ObservableCollection<Point>();
Plotter.DataContext = this;
}
private void button_Click(object sender, RoutedEventArgs e)
{
double[] my_array = new double[10];
for (int i = 0; i < my_array.Length; i++)
{
my_array[i] = Math.Sin(i);
Data.Add(new Point(i, my_array[i]));
}
}
}
有人能告诉我怎么做吗?可能我必须在XAML中添加一些内容,例如ItemsSource =&#34; Data&#34;但我找不到那个。 谢谢。
答案 0 :(得分:2)
使用Plotter.AddLineGraph(Data);
:
using Microsoft.Research.DynamicDataDisplay;
using Microsoft.Research.DynamicDataDisplay.DataSources;
private void button_Click(object sender, RoutedEventArgs e)
{
double[] my_array = new double[10];
for (int i = 0; i < my_array.Length; i++)
{
my_array[i] = Math.Sin(i);
Data.Collection.Add(new Point(i, my_array[i]));
}
Plotter.AddLineGraph(Data);
}
编辑:以下是使用MVVM的完整工作代码,因此您无需使用AddLineGraph
:
XAML:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication1"
xmlns:d3="http://research.microsoft.com/DynamicDataDisplay/1.0"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
<Grid>
<d3:ChartPlotter>
<d3:LineGraph DataSource="{Binding Data}"></d3:LineGraph>
</d3:ChartPlotter>
</Grid>
CS:
public partial class MainWindow : Window
{
MyViewModel viewModel;
public MainWindow()
{
InitializeComponent();
viewModel = new MyViewModel();
DataContext = viewModel;
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
double[] my_array = new double[10];
for (int i = 0; i < my_array.Length; i++)
{
my_array[i] = Math.Sin(i);
viewModel.Data.Collection.Add(new Point(i, my_array[i]));
}
}
}
视图模型:
using Microsoft.Research.DynamicDataDisplay.DataSources;
public class MyViewModel
{
public ObservableDataSource<Point> Data { get; set; }
public MyViewModel()
{
Data = new ObservableDataSource<Point>();
}
}
答案 1 :(得分:0)
我不知道在XAML中声明的命名空间:
xmlns:d3="http://research.microsoft.com/DynamicDataDisplay/1.0"
但我可以添加一些建议。确保Point类将坐标值X和Y公开为公共属性。之后,您必须确保控件ChartPlotter支持XAML绑定。