Setting Canvas properties in an ItemsControl DataTemplate

狂风中的少年 提交于 2019-11-26 05:56:05

问题


I\'m trying to databind to this ItemsControl:

<ItemsControl ItemsSource=\"{Binding Path=Nodes, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}\">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <Canvas />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
</ItemsControl>

By using this DataTemplate, I\'m trying to individually position my Node elements on the Canvas correctly:

<DataTemplate DataType=\"{x:Type Model:EndNode}\">
    <Controls:EndNodeControl Canvas.Left=\"{Binding Path=XPos}\" Canvas.Top=\"{Binding Path=YPos}\" />
</DataTemplate>

However, it\'s not working as expected. All my node elements are drawn on top of each other in the same position. Any suggestions on how to accomplish this?


回答1:


The attached properties only work on direct children of the Canvas. ItemsControl will place ContentPresenter controls as its direct children, so you might want to add a style for that as well:

<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>

Hope this helps



来源:https://stackoverflow.com/questions/1265364/setting-canvas-properties-in-an-itemscontrol-datatemplate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!