2016-07-23 40 views
0

我正在尝试使用动态数据显示来制作图表。我正在使用MVVM。我senderviewmodel代码如下使用mvvm的绑定图绘图仪

 voltagePointCollection = new VoltagePointCollection(); 

     updateCollectionTimer = new DispatcherTimer(); 
     updateCollectionTimer.Interval = TimeSpan.FromMilliseconds(1); 
     updateCollectionTimer.Tick += new EventHandler(updateCollectionTimer_Tick); 
     updateCollectionTimer.Start(); 

     var ds = new EnumerableDataSource<VoltagePoint>(voltagePointCollection); 
     ds.SetXMapping(x => dateAxis.ConvertToDouble(x.Date)); 
     ds.SetYMapping(y => y.Voltage); 
     ((MainWindow)System.Windows.Application.Current.MainWindow).TextBlock1.Text = "Setting Text from My Program"; 
     ((MainWindow)System.Windows.Application.Current.MainWindow).plotter.AddLineGraph(ds, Colors.Green, 2, "Volts"); 
     plotter.AddLineGraph(ds, Colors.Green, 2, "Volts"); // to use this method you need "using Microsoft.Research.DynamicDataDisplay;" 

     MaxVoltage = 3; 
     MinVoltage = -3; 
     //System.IO.StreamReader file = new System.IO.StreamReader("C:\\Users\\niranjan\\Desktop\\sample.txt"); 
     //var sample = file.ReadToEnd(); 
     //tokens = sample.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None); 
     // here graph ending 

     ResultValue = "N/D"; 
     var wasSent = await _senderBluetoothService.Send(SelectDevice, Data); 
     if (wasSent) 
     { 
      ResultValue = "The data was sent."; 
     } 
     else 
     { 
      ResultValue = "The data was not sent."; 
     } 

我view.xaml文件如下

<d3:ChartPlotter Name= "plotter" Grid.Row="1" Grid.Column="1" Height="244" HorizontalAlignment="Left" Width="1076"> 
     <d3:ChartPlotter.HorizontalAxis> 
      <d3:HorizontalDateTimeAxis Name="dateAxis"/> 
     </d3:ChartPlotter.HorizontalAxis> 
     <d3:Header FontFamily="Georgia" Content="Voltage chart"/> 
     <d3:VerticalAxisTitle FontFamily="Georgia" Content="Voltage [V]" /> 
     <d3:HorizontalAxisTitle FontFamily="Georgia" Content="Time"/> 
     <d3:HorizontalLine Value="{Binding MaxVoltage}" Stroke="Red" StrokeThickness="2"/> 
     <d3:HorizontalLine Value="{Binding MinVoltage}" Stroke="Red" StrokeThickness="2"/> 
    </d3:ChartPlotter> 

的问题是在该行

plotter.AddLineGraph(ds, Colors.Green, 2, "Volts"); 

上面说的是绘图仪不存在当前的情况。但绑定MaxVoltage和MinVoltage的工作。您能否告诉我一些更改,以便我可以在SenderViewmodel.cs文件中使用plotter.addline。

回答

0

使用MVVM时,不能直接定位UI元素。与您的用户界面进行交互的唯一方式是使用ViewModel属性并将其与用户界面绑定。 你是这样做的最大和MinVoltage,所以那些工作...

但绘图仪是一个UI元素,如果你想使用它的任何方法,最好的方法是通过使用某种MVVM消息传递(mvvm light messenger)并在视图后面的代码中注册该消息。这样你可以使用UI元素。

0

您可以在视图中声明你的视图模型的实例,并将其绑定到视图的DataContext的如下:

public MyViewModel myVM; 

添加到您的视图的构造函数:

public MyView() 
    { 
    .. 
    myVM = new MyViewModel(); 

    this.DataContext = myVM; 
    } 

声明一个绘图仪内的视图和这里使用您的viewModel数据作为数据源,然后调用绘图功能:

var ds = new EnumerableDataSource<VoltagePoint>(myVM.voltagePointCollection); 
     ds.SetXMapping(x => dateAxis.ConvertToDouble(x.Date)); 
     ds.SetYMapping(y => y.Voltage); 
     plotter.AddLineGraph(ds, Colors.Green, 2, "Volts"); 

希望它有一点帮助。你有问题我仍然在你的处置。

此致敬礼,