1

我在将一些 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
4

1 回答 1

0

排序:-)

重要信息在此链接中: 如何使用 wpf 中的两个文本框在线系列图中绘制值

似乎有两个微妙之处 - 似乎都不能孤立地解决问题,但两者的结合确实做到了:

基本上,当您在 VB WPF 窗口上拖放 WPF Toolkit 图表时,它会添加 xmlns 条目:

    xmlns:chartingToolkit="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit" x:Class="MainWindow"

标题="主窗口" 高度="350" 宽度="525">

并且您的图表定义为

    <chartingToolkit:Chart x:Name="MyChart"    ...>
    </chartingToolkit:Chart>

这一切似乎都编译好了,但我的 VB 代码不会显示这些点。请注意以下代码中的不同 xmlns,它们似乎工作得很好。这可能是因为我的开发 PC 上有多个开发环境?

另请注意,在此示例中,为图表设置了数据上下文,而不是线系列。我的下一个挑战是看看当我尝试显示多行时会发生什么!

我还应该补充一点,轴定义方式的变化并不是让它表现的原因,这只是探索的众多途径之一。文本框和按钮只是为了演示添加点-我不为布局中缺乏技巧而道歉,我只是想让它工作!

xml:

<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Toolkit="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit" 
x:Class="MainWindow"
Title="MainWindow" Height="352.357" Width="803.114">
<Grid>


    <Toolkit:Chart Name="MyChart" 
                   Title="Line Series Demo" 
                   VerticalAlignment="Top" 
                   Height="320"
                   Width="500" Margin="32,0,263,0">
        <Toolkit:Chart.Axes>
            <!-- Shared horizontal axis -->
            <Toolkit:LinearAxis
                            Orientation="X"
                            Title="Detector px"
                            Interval="64"
                            Minimum="0"
                            Maximum="256"
                            ShowGridLines="True"/>
            <Toolkit:LinearAxis
                                Orientation="Y"
                                Title="Intensity"
                                Minimum="0"
                                Maximum="65535"
                                Interval="8000"
                                ShowGridLines="True"/>
        </Toolkit:Chart.Axes>

        <Toolkit:LineSeries  DependentValuePath="Intensity" 
                             IndependentValuePath="PxNum" 
                             ItemsSource="{Binding UpdateSourceTrigger=PropertyChanged}" 
                             IsSelectionEnabled="True"/>
    </Toolkit:Chart>

    <TextBox x:Name="txtX" HorizontalAlignment="Left" Height="25" Margin="550,10,0,0" TextWrapping="Wrap" Text="0" VerticalAlignment="Top" Width="78"/>
    <TextBox x:Name="txtY" HorizontalAlignment="Left" Height="25" Margin="648,10,0,0" TextWrapping="Wrap" Text="0" VerticalAlignment="Top" Width="78"/>
    <Button x:Name="butAdd" Content="Add Point" HorizontalAlignment="Left" Height="29" Margin="648,40,0,0" VerticalAlignment="Top" Width="78"/>

</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 = 4000}) '   New KeyValuePair(Of Int32, Int32)(nPX, 54000))


    MyChart.DataContext = Series_Saturation

End Sub



Private Sub butAdd_Click(sender As Object, e As RoutedEventArgs) Handles butAdd.Click

    Dim X As Int32 = CInt(txtX.Text)
    Dim Y As Int32 = CInt(txtY.Text)

    Series_Saturation.Add(New GraphPoint() With {.PxNum = X, .Intensity = Y}) 

End Sub
End Class


Public Class GraphPoint

    Public Property PxNum As Long
    Public Property Intensity As Long

End Class

我真诚地希望这对其他人有所帮助!

于 2014-08-01T10:33:59.767 回答