在ItemsControl DataTemplate中设置Canvas属性

我试图绑定到这个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> 

但是,它没有按预期工作。 我所有的节点元素都在同一个位置上绘制。 有关如何完成此任何build议?

附加属性仅适用于Canvas的直接子项。 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> 

希望这可以帮助