Search code examples
wpfvb.netgraphicsdraw

WPF Update lines


I'm trying to replicate in wpf(vb.net) an app that I made some times ago in winform. Il the old app I basically open a binary file and load it in one array and then draw a 2d lines chart with it. Files are like 2/4MB size, so i put a slider and every time I change it I call the drawing sub with the slider offset. Now I'm totaly new in wpf, I figure out how to draw the chart, but I don't understand how update it when I move the slider, or(next step) when I change array's values by code. To make simple tests I load a file when start and when button1 clicked I modify array values and I would see the update chart, but I have not found a way.

    <Window x:Class="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:testwpf2d"
        mc:Ignorable="d"
        Title="MainWindow" Height="1920" Width="1080">
    <Grid x:Name="BaseGrid">
        <Canvas  Name="paintSurface" >
            <Canvas.Background>
                <SolidColorBrush Color="Black" Opacity="1"/>
            </Canvas.Background>
            <Button Content="Button" Canvas.Left="184" Canvas.Top="36" Width="75" Click="Button_Click"/>
        </Canvas>
    </Grid>
</Window>


Private Sub BaseGrid_Loaded(sender As Object, e As RoutedEventArgs) Handles BaseGrid.Loaded
    Dim openFileDialogORI As OpenFileDialog = New OpenFileDialog()

    If openFileDialogORI.ShowDialog = True Then
        Try
            Dim MyFileORIStream As FileStream = New FileStream(openFileDialogORI.FileName, FileMode.Open, FileAccess.Read)
            Dim ORIBuffer As BinaryReader = New BinaryReader(MyFileORIStream)
            Dim fInfo As New FileInfo(openFileDialogORI.FileName)
            Dim numBytes As Long = fInfo.Length

            MyORIArray = ORIBuffer.ReadBytes(CInt(numBytes))

            ORIBuffer.Close()
            MyFileORIStream.Close()
          
            Dim NomeFileOri As String = openFileDialogORI.FileName
            Dim info As New FileInfo(openFileDialogORI.FileName)
            Dim length As Integer = CInt(info.Length)                
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End If
    MessageBox.Show("File loaded")

    For i As Integer = 1 To 2000
        Dim Line As New Line
        Line.X1 = i
        Line.X2 = i + 1
        Line.Y1 = MyORIArray(i)
        Line.Y2 = MyORIArray(i + 1)
        Line.Stroke = Brushes.YellowGreen
        Line.StrokeThickness = 0.25
        BaseGrid.Children.Add(Line)
    Next
End Sub

Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
    Dim Rand As New Random

    For i As Integer = 1 To 1000
        MyORIArray(i) = Rand.NextDouble * 50
    Next
    MessageBox.Show("new data ")
    BaseGrid.UpdateLayout()
    paintSurface.UpdateLayout()
End Sub

Solution

  • I solved creating a collection of point from the array and draw a polyline, when arraypoint change(always by user interaction that raise an event(button,mouse etc), I update or recreate point collection and then use update.layout. I understand that is not the best way but for this little application work good