I'm working on a Windows 8 app. I have to place several controls (images, rectangles) inside a Canvas. If I do it directly, everything is fine when I'm using "Canvas.Left" attached property on the children (the mentioned controls). However, I'd like to use MVVM. Therefore I now use an ItemsControl like this:
<ItemsControl ItemsSource="{Binding MyObjects}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<Image Source="{Binding Image}" Visibility="{Binding IsImage, Converter={StaticResource boolConverter}}" />
<Rectangle Canvas.Left="{Binding Left}" Canvas.Top="{Binding Top}" Width="{Binding Width}" Height="{Binding Height}" Fill="{Binding Fill}" Visibility="{Binding IsNoImage, Converter={StaticResource boolConverter}}" />
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Binding is working except for the attached Canvas properties. After some research I found out that's because of ItemsControl wrapping its children with a ContentPresenter. Therefore I tried to make use a solution I found on stackoverflow, but without success:
<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<Setter Property="Canvas.Left" Value="{Binding Left}" />
</Style>
</ItemsControl.ItemContainerStyle>
Adding this code doesn't set the Canvas.Left property (even after removing the binding from the DataTemplate).
What am I missing? Thanks in advance.