我使用CompositeCollection
主办我想在ItemsControl
控件来显示对象的ItemsControl,我用this解决方案使用不同的DataTemplates为不同的对象来实现,但是我想申请每种类型的在我的收藏中有不同的风格。我怎样才能做到这一点?应用不同的风格
这是我的代码:
<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>
运行这个实际的结果是,MapObjects的将在Canvas
正确的位置被显示,但该ReferenceMapRectangle
对象将保持固定在(0,0)在画布上,永远不会移动(宽度/高度确实会更新)
有没有人有线索为什么会发生这种情况?我尝试使用ItemsControl.ItemContainerStyle
,但它只支持一种样式,而不支持多种样式。
谢谢!
难道它是一个绑定问题而不是样式问题?检查你的调试输出窗口。 –
不要这样想,因为我的宽度/高度绑定正确。我认为DataTemplate.Resources中的样式不可访问。代码仅查看ItemsControl.ItemContainerStyle代码... –