【发布时间】:2014-06-09 23:54:41
【问题描述】:
我正在用 C# 编写一个 WPF 应用程序。此应用程序是使用 MVVM 设计的。
目前,我有一个带有几个复选框的父窗口。使用用户可以检查他们想要的任何框,然后单击“绘图”Button。一旦他们点击“绘图”,就会出现一个新的子窗口,在 单个 图表上显示数据。
所以,如果我只选中了一个复选框,然后单击“绘图”,我将看到一个带有单线的图形。如果我有 2 个复选框选中并单击“绘图”,我将看到相同的单个图形,但上面会有 2 条线。
我目前的实现:
目前,我有一个名为GraphWindowView 的“视图”类。视图显然需要知道要显示哪些数据。所以要做到这一点,我有 GraphWindowView.Dates 和 GraphWindowView.Data 的依赖属性,它们最终会生成 Data(y 轴)与 Dates(x 轴)的图表。
问题:GraphWindowView 的当前实现显然仅限于绘制一组数据(即Data 与Dates)。我想让这个(很多)更具可扩展性,并根据选中的复选框数量来提供任意数量的绘图。我该怎么做呢?我想我需要重新考虑我对依赖属性的使用......
>>> 更新
所以我创建了一个GraphLine 类,它应该代表图表上的一条线。 “图”实际上是GraphWindowPresenter.xaml 类中的ChartPlotter 元素。此外,我为GraphLine 对象指定了DataType,但这就是我所理解的。下一步是什么,我如何实际将数据添加到图表中?以及如何/在哪里创建 GraphLine 的实例来填充 ChartPlotter 元素?抱歉,即使在阅读了很多教程之后,我也很迷茫。感谢您迄今为止的所有帮助,我真的很感激!
GraphWindowView.xaml
<Window x:Class="BMSVM_Simulator.View.GraphWindowView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ViewModel="clr-namespace:BMSVM_Simulator.ViewModel"
xmlns:d3="http://research.microsoft.com/DynamicDataDisplay/1.0"
x:Name="ThisGraphWindowInstance"
Title="Plot" Height="500" Width="750"
Icon="../res/qualcomm_q_icon.ico.ico"
MinWidth="400" MinHeight="300">
<Window.DataContext>
<ViewModel:GraphWindowPresenter/>
</Window.DataContext>
<d3:ChartPlotter Name="plotter" Margin="10,10,20,10">
<d3:ChartPlotter.HorizontalAxis>
<d3:HorizontalIntegerAxis Name="dateAxis"/>
</d3:ChartPlotter.HorizontalAxis>
<d3:ChartPlotter.VerticalAxis>
<d3:VerticalIntegerAxis Name="countAxis"/>
</d3:ChartPlotter.VerticalAxis>
<d3:Header FontFamily="Arial" Content="{Binding ElementName=ThisGraphWindowInstance, Path=title}"/>
<d3:VerticalAxisTitle FontFamily="Arial" Content="{Binding ElementName=ThisGraphWindowInstance, Path=yAxis}"/>
<d3:HorizontalAxisTitle FontFamily="Arial" Content="{Binding ElementName=ThisGraphWindowInstance, Path=xAxis}"/>
</d3:ChartPlotter>
<Window.Resources>
<DataTemplate DataType="{x:Type ViewModel:GraphLine}">
<!--WHAT GOES HERE-->
</DataTemplate>
</Window.Resources>
</Window>
GraphLine.cs
namespace BMSVM_Simulator.ViewModel
{
class GraphLine
{
public string xAxis { get; private set; }
public string yAxis { get; private set; }
public string title { get; private set; }
public string legend { get; private set; }
public EnumerableDataSource<int> data { get; private set; }
public EnumerableDataSource<int> dates { get; private set; }
}
}
【问题讨论】:
标签: c# wpf xaml dynamic-data-display