Dylan Vester
Dylan Vester

Reputation: 2726

Databinding View Model Lines Collection to Canvas (MVVM / Windows Phone)

I'm attempting to allow users to draw on a Canvas by collecting the points as they tap and drag. I'm using an MVVM approach and can successfully collect points, and populate a collection of lines on the View Model. However, I need to be able to display those line controls on a canvas so the user can see the line they are drawing.

My question is, how can I databind the View Models collection of Line controls to the canvas's Children collection?

Upvotes: 0

Views: 533

Answers (1)

ColinE
ColinE

Reputation: 70142

This can be achieved using an ItemsControl:

<ItemsControl ItemsSource="{Binding CollectionOfLines}">
  <!-- specify the panel that the items will be added to -->
  <ItemsControl.ItemsPanel>
    <ItemsPanelTemplate>
      <Canvas/>
    </ItemsPanelTemplate>
  </ItemsControl.ItemsPanel>
  <ItemsControl.ItemTemplate>
    <DataTemplate>
      <Line X="{Binding ...}" Y="{Binding ...}" .../>
    </DataTemplate>
  </ItemsControl.ItemTemplate>
</ItemsControl>

Upvotes: 1

Related Questions