Development Tip

ItemsControl DataTemplate에서 캔버스 속성 설정

yourdevel 2020. 11. 1. 18:44
반응형

ItemsControl DataTemplate에서 캔버스 속성 설정


나는 이것에 데이터 바인딩을 시도하고있다 ItemsControl:

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

this를 사용하여 DataTemplateNode요소를 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>

도움이 되었기를 바랍니다

참고 URL : https://stackoverflow.com/questions/1265364/setting-canvas-properties-in-an-itemscontrol-datatemplate

반응형