2016-06-14 5 views
0

Windows 8.1 Metro 앱에서 내 뷰 모델의 셰이프 모음을 MainPage.xaml에 바인딩하려고합니다. 각 도형은 Left, TopPathData을 가지며 해당 위치에서 캔버스 내부에 그려지는 직사각형을 포함하는 RectangleGeometry이됩니다.캔버스 안의 모양/컨트롤의 절대 좌표를 바인딩하는 방법

<Grid Background="Black"> 
    <ItemsControl ItemsSource="{Binding Shapes}"> 
     <ItemsControl.ItemsPanel> 
      <ItemsPanelTemplate> 
       <Canvas /> 
      </ItemsPanelTemplate> 
     </ItemsControl.ItemsPanel> 
     <ItemsControl.ItemContainerStyle> 
      <Style TargetType="ContentPresenter"> 
       <Setter Property="Canvas.Top" Value="{Binding Top}"/> 
       <Setter Property="Canvas.Left" Value="{Binding Left}"/> 
      </Style> 
     </ItemsControl.ItemContainerStyle> 
     <ItemsControl.ItemTemplate> 
      <DataTemplate> 
       <Path Data="{Binding PathData}" Stroke="White" StrokeThickness="3" Canvas.Left="{Binding Left}" Canvas.Top="{Binding Top}"/> 
      </DataTemplate> 
     </ItemsControl.ItemTemplate> 
    </ItemsControl> 
</Grid> 

데이터 컨텍스트가 설정되고 제대로 작동 :

은 XAML입니다. MainViewModel에서 도형을 채우고 사각형이 화면에 나타나지만 문제는 정확히 Left 및 안에있는 Top 위치에 배치 할 사각형을 가져올 수 없다는 것입니다. 즉, 방금 (0,0).

는 I 시도 결합 모두 PathCanvas.LeftCanvas.Top (I 시도 명백한 방법)과 동일한 기능을 수행하도록되어있는 Style (I은 WPF 예에서 발견하는 방법)과 함께 ItemContainerStyle 설정. 하지만 둘 다 작동하지 않습니다 (참조 용 xaml에 두 방법을 모두 추가했습니다).

그래서 내가 뭘 잘못하고 있으며 해당 위치에 사각형을 표시하려면 어떻게해야합니까?


편집 : 내 질문은 내가 타겟팅하는 것을 윈도우 메트로/UWP가되는 것을 허용 대답이 작동하지 않습니다 제외 this one for WPF과 동일합니다.

답변

2

대신 Transform에 바인딩하여 문제를 해결했습니다.

<Path Data="{Binding PathData}" Stroke="White" StrokeThickness="3"> 
    <Path.RenderTransform> 
     <CompositeTransform TranslateX="{Binding Left}" TranslateY="{Binding Top}"/> 
    </Path.RenderTransform> 
</Path>