[c#] ItemsControl DataTemplate에서 캔버스 속성 설정

나는 이것에 데이터 바인딩을 시도하고있다 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>

그러나 예상대로 작동하지 않습니다. 모든 노드 요소는 동일한 위치에서 서로 위에 그려집니다. 이를 수행하는 방법에 대한 제안 사항이 있습니까?



답변

연결된 속성은 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>


답변