在一个ItemsControl DataTemplate中设置画布属性
-
13-09-2019 - |
题
我想数据绑定到这个ItemsControl
:
<ItemsControl ItemsSource="{Binding Path=Nodes, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
通过使用此DataTemplate
,我想单独定位在正确的Node
我Canvas
元素:
<DataTemplate DataType="{x:Type Model:EndNode}">
<Controls:EndNodeControl Canvas.Left="{Binding Path=XPos}" Canvas.Top="{Binding Path=YPos}" />
</DataTemplate>
不过,由于预期它不工作。我所有的节点元素被绘制在同一个位置彼此顶部。如何做到这一点有什么建议?
解决方案
在附加属性仅在画布的直接子工作。 ItemsControl中将会把ContentPresenter控件的直接孩子,所以你可能要添加一个样式为以及:
<ItemsControl ItemsSource="{Binding Path=Nodes}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<Setter Property="Canvas.Left" Value="{Binding Path=XPos}" />
<Setter Property="Canvas.Top" Value="{Binding Path=YPos}" />
</Style>
</ItemsControl.ItemContainerStyle>
</ItemsControl>
希望这有助于
不隶属于 StackOverflow