c# - 使用 ObservableCollection 进行动态数据显示

标签 c# wpf xaml charts dynamic-data-display

我不知道如何使用 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="Data",但我找不到那个。 谢谢。

最佳答案

使用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);
            }

enter image description here

编辑:这是我使用 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]));
            }
        }
    }

View 模型:

using Microsoft.Research.DynamicDataDisplay.DataSources;

public class MyViewModel
    {
        public ObservableDataSource<Point> Data { get; set; }

        public MyViewModel()
        {
            Data = new ObservableDataSource<Point>();
        }
    }

关于c# - 使用 ObservableCollection 进行动态数据显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38441575/

相关文章:

c# - 如何在 C# 中格式化显示在 MessageBox 中的字符串?

c# - DependencyProperty 未在 NotifyCollectionChanged 事件上通知 UI

c# - MsiExec 进度捕捉

c# - 如何使用 C# 或 SQL 将两列合并为一列

c# - 组合两个表达式 (Expression<Func<T, bool>>)

c# - Silverlight - 如何在 XAML 中进行国际化?

c# - 如何从 XAML 中正确引用类

c# - 未发现 NUnit v3 单元测试 - "Discover test finished: 0 found"

wpf - 如何在 WPF DataGrid 中将 XML 文件中的文本排序为数字?

wpf - WPF 中 DataGridColumn 的绑定(bind)可见性