0

I am an absolute newbie to c#, WPF and MVVM and I and trying to create a canvas were I can add points based on an observable list's points x and y coordinates. I created the custome user control and this is my view model:

public  class ViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public ViewModel()
    {
        points = new ObservableCollection<Point>();
        this.testData();
    }

    public ObservableCollection<Point> points { get; set; }

    private void testData()
    {
       // points.Add(new Point(0, 50) );
        //points.Add(new Point(50, 0));
        //points.Add(new Point(13, 73));
        //points.Add(new Point(12, 23));
        //points.Add(new Point(34, 80));
        //points.Add(new Point(322, 225));
       //points.Add(new Point(270, 510));
        points.Add(new Point(0, 0));
    }
}

This is what I have on my mainWindows.xaml

<Canvas>
  <local:UserControl2 />
</Canvas>

your clarification is much appriciated

Jose Gonzalez
  • 49
  • 1
  • 7
  • This might provide some insight http://stackoverflow.com/questions/5913792/wpf-canvas-how-to-add-children-dynamically-with-mvvm-code-behind – Eldho Apr 12 '17 at 13:32
  • You need an ItemsControl that has a Canvas as it's Panel. Then you bind ItemsSource to your observable collection. Inside ItemsControl.Resources you would create a DataTemplate for type Point in which you place your UI (I dunno, a 1x1 Rectangle?). You would then bind Canvas.Left and Canvas.Top on this UI to your Point's X and Y properties. On binding, the ItemsControl will take each item in its ItemsSource, look for a DataTemplate that matches the type, and if found, create the UI via the template, set its DataContext to the item, and stick it in the panel--the Canvas. It's like magic. –  Apr 12 '17 at 14:28

0 Answers0