我在将一些 VB.Net winforms 代码移植到 WPF 时遇到了相当令人沮丧的事情,并且可以通过一些快速的帮助来完成:
简而言之,我有动态生成的数据,我需要在线条上绘制。无论我尝试什么,图表都顽固地拒绝显示我的数据!我已经搞砸了 .DataContext / .ItemsSource / Bindings / 等的几乎所有组合。我可以找到并且有一个认真的谷歌,但好的 VB.Net 示例似乎很薄弱。我显然错过了一些“简单”的东西......任何建议都会受到欢迎。
截断代码如下:
XAML:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:chartingToolkit="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit" x:Class="MainWindow"
Title="MainWindow" Height="350" Width="525">
<Grid>
<chartingToolkit:Chart x:Name="MyChart" HorizontalAlignment="Left" Margin="10,10,0,0" Title="Chart Title" VerticalAlignment="Top" Height="300" Width="497">
<chartingToolkit:LineSeries x:Name="MyLineSeries" DependentValueBinding="{Binding Path=Intensity}" IndependentValueBinding="{Binding Path=PxNum}" IsSelectionEnabled="True" ItemsSource="{Binding}" >
<!-- Vertical axis for Intensity values -->
<chartingToolkit:LineSeries.DependentRangeAxis>
<chartingToolkit:LinearAxis
Orientation="Y"
Title="Intensity"
Minimum="0"
Maximum="65535"
Interval="8000"
ShowGridLines="True"
/>
</chartingToolkit:LineSeries.DependentRangeAxis>
</chartingToolkit:LineSeries>
<chartingToolkit:Chart.Axes>
<!-- Shared horizontal axis -->
<chartingToolkit:LinearAxis
Orientation="X"
Title="Detector px"
Interval="64"
Minimum="0"
Maximum="256"
ShowGridLines="True"/>
</chartingToolkit:Chart.Axes>
</chartingToolkit:Chart>
</Grid>
</Window>
VB:
Imports System.Collections.ObjectModel
Class MainWindow
Dim Series_Saturation As New ObservableCollection(Of GraphPoint)
Private Sub MainWindow_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
Series_Saturation.Add(New GraphPoint() With {.PxNum = 0, .Intensity = 54000}) ' New KeyValuePair(Of Int32, Int32)(0, 54000))
Series_Saturation.Add(New GraphPoint() With {.PxNum = 200, .Intensity = 54000}) ' New KeyValuePair(Of Int32, Int32)(nPX, 54000))
MyLineSeries.DataContext = Series_Saturation
End Sub
End Class
Public Class GraphPoint
Public Property PxNum As Long
Public Property Intensity As Long
End Class