[英]Applying different styles to ItemsControl
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. 我正在使用
CompositeCollection
承载要显示在ItemsControl
控件中的对象,我使用了此解决方案来为不同的对象使用不同的DataTemplates来实现,但是我想为集合中的每种类型应用不同的样式。 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) 运行此命令的实际结果是MapObjects将显示在
Canvas
上的正确位置,但是ReferenceMapRectangle
对象将固定在画布上的(0,0)中,并且永远不会移动(尽管宽度/高度会更新,但是)
Does anyone have a clue why this could happen? 有人知道为什么会发生这种情况吗? I tried using
ItemsControl.ItemContainerStyle
but it only supports one style, and not multiple styles. 我尝试使用
ItemsControl.ItemContainerStyle
但它仅支持一种样式,而不支持多种样式。
Thanks! 谢谢!
Each Item in an ItemsControl
is wrapped in a <ContentPresenter>
tag, so applying your positioning on your actual DataItem will do nothing. ItemsControl
每个Item都包裹在<ContentPresenter>
标记中,因此将位置应用于实际的DataItem不会执行任何操作。
You can use ItemContainerStyle
to apply the positioning on the ContentPresenter
tag instead of your DataItem tag 您可以使用
ItemContainerStyle
在ContentPresenter
标记而不是DataItem标记上应用定位
For example, here is how your controls get rendered if you apply positioning to your DataItem
: 例如,这是将定位应用于
DataItem
控件的呈现方式:
<Canvas>
<ContentPresenter>
<DataItem Canvas.Left="50" Canvas.Top="50" />
</ContentPresenter>
<ContentPresenter>
<DataItem Canvas.Left="100" Canvas.Top="50" />
</ContentPresenter>
<ContentPresenter>
<DataItem Canvas.Left="150" Canvas.Top="50" />
</ContentPresenter>
</Canvas>
And here is how your controls would get rendered if you apply positioning in the ItemContainerStyle
: 如果您在
ItemContainerStyle
应用定位,则这是呈现控件的方式:
<Canvas>
<ContentPresenter Canvas.Left="50" Canvas.Top="50">
<DataItem />
</ContentPresenter>
<ContentPresenter Canvas.Left="100" Canvas.Top="50">
<DataItem />
</ContentPresenter>
<ContentPresenter Canvas.Left="150" Canvas.Top="50">
<DataItem />
</ContentPresenter>
</Canvas>
See this blog entry of mine for another example 有关其他示例,请参见我的此博客条目。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.