I'm using a CompositeCollection
to host objects I want to display in a ItemsControl
control, I used this solution to implement using different DataTemplates for the different objects, however I want to apply for each of the types in my collection a different style. How can I do this?
This is my code:
<ItemsControl.Resources>
<DataTemplate DataType="{x:Type mapNamespace:MapObject}">
<DataTemplate.Resources>
<Style TargetType="{x:Type ContentPresenter}">
<Setter Property="Canvas.Left" Value="{Binding MapObjLocation.X}" />
<Setter Property="Canvas.Top" Value="{Binding MapObjLocation.Y}" />
</Style>
</DataTemplate.Resources>
<Rectangle Fill="#00000000" Height="10" Width="10" Stroke="Red">
<Rectangle.ToolTip>
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Horizontal">
<TextBlock Text="X: "/>
<TextBlock Text="{Binding MapObjLocation.X}" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Y: " />
<TextBlock Text="{Binding MapObjLocation.Y}" />
</StackPanel>
</StackPanel>
</Rectangle.ToolTip>
</Rectangle>
</DataTemplate>
<DataTemplate DataType="{x:Type viewModel:ReferenceMapRectangle}">
<DataTemplate.Resources>
<Style TargetType="{x:Type ContentPresenter}">
<Setter Property="Canvas.Left" Value="{Binding X}" />
<Setter Property="Canvas.Top" Value="{Binding Y}" />
</Style>
</DataTemplate.Resources>
<Rectangle Height="{Binding Height, Mode=TwoWay}" Width="{Binding Width, Mode=TwoWay}" Stroke="White" StrokeThickness="6"
Canvas.Top="{Binding Y, Mode=TwoWay}" Canvas.Left="{Binding X, Mode=TwoWay}" >
</Rectangle>
</DataTemplate>
</ItemsControl.Resources>
The actual result from running this is that the MapObjects will be shown in the correct location on the Canvas
, but the ReferenceMapRectangle
object will stay fixed in (0,0) on the canvas and will never move (the width / height does update, though)
Does anyone have a clue why this could happen? I tried using ItemsControl.ItemContainerStyle
but it only supports one style, and not multiple styles.
Thanks!