Click here to Skip to main content
15,889,116 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In ViewModel class, I have ObservableCollection<List<point>> MyList as property. in xaml, I need to extract x axis y axis values to draw lines from Mylist. I have already set DataContext to VM instance in xaml.

What I have tried:

<ScrollViewer Name="scrollViewer" HorizontalScrollBarVisibility="Auto"   Grid.Row="2" Grid.Column="3" 
                    Height= "600" Width="Auto">
            <ItemsControl ItemsSource="{Binding Path=MyList}">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <Canvas x:Name="front_canvas" Background="White"  Height="1200" Width="{Binding CanvasWidth}" />
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Line X1="{Binding Path=LinePoints}" Y1="{Binding LinePoints}" 
                            X2="{Binding LinePoints}" Y2="{Binding LinePoints}"
                              Stroke="Black" StrokeThickness="1">
                        </Line>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </ScrollViewer>
Posted
Updated 19-Nov-18 6:12am

I found potential help for you on stackoverflow:
data binding - Bind collection of lines to canvas in WPF - Stack Overflow[^]

Looks like you didn't finish filling in the bindings for the points when drawing the line, at the least, and you may want to use its solution to create a custom Line to keep all of the data in a logical structure.
 
Share this answer
 
The optimal solution depends on your problem, but I did things a little different since one nearly always want the Y axis inverted when plotting.
A simple WPF LineChart control[^]

HTML
<ItemsControl x:FieldModifier="private" x:Name="PlotArea" Canvas.Bottom="40" Canvas.Left="40"  ClipToBounds="True"  ItemsSource="{Binding}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <Canvas >
                <Canvas.LayoutTransform>
                    <ScaleTransform ScaleX="1" ScaleY="-1"></ScaleTransform>
                </Canvas.LayoutTransform>
            </Canvas>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
</ItemsControl>


My problem was that the observable collection could not be bonded directly to the Points in the PolyLine. So I ended up writing some code that did this manually. Or you could do the approach that is already suggested.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900