2

I'm new to WPF and trying to databind shapes to a canvas. I have an observableCollection of "cake" objects in my viewmodel. I've managed to make databinding work and write a list with my cakeID's, but would like to add the cakes to a canvas as ellipses and from searching I've found itemsControl to be the only solution. As I have an observableCollection of cakes and not an observableCollection of objects I'm struggling to do this. Should I create the ellipse object in the cake object or can I just have the data and create it in XAML?

My XAML code looks like this, but I'm confused with what I should bind where, to retrieve the ellipse from the cake objects in the cake list, and the XPos and YPos

<DockPanel>
    <Viewbox Margin="30,30,30,42">
        <ItemsControl ItemsSource="{Binding Path=cakes}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <Canvas Name="canvasWorld" Background="Blue" Width="3996.0" Height="2009.1" IsItemsHost="True">
                    </Canvas>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Path Data="{Binding ellipse}"/>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
            <ItemsControl.ItemContainerStyle>
                <Style>
                    <Setter Property="Canvas.Top" Value="{Binding Path=XPos}" />
                    <Setter Property="Canvas.Left" Value="{Binding Path=YPos}" />
                </Style>
            </ItemsControl.ItemContainerStyle>
        </ItemsControl>
    </Viewbox>
</DockPanel>
daniele3004
  • 13,072
  • 12
  • 67
  • 75
TBH
  • 23
  • 1
  • 3
  • you should of course save the data in your viewmodel. From that data, the XAML will render the Ellipse for you. If you mean you want to access the Ellipse from code behind, then you have to access the `Path` because there is not any Ellipse shape here, you used `Path` for `ItemTemplate`. – King King Nov 07 '14 at 10:33
  • [This answer](http://stackoverflow.com/a/22325266/1136211) might be helpful. Replace Rectangle by Ellipse. – Clemens Nov 07 '14 at 10:57
  • Thank you very much! The example helped a lot! – TBH Nov 07 '14 at 13:10

0 Answers0