2016-06-14 82 views
0

在Windows 8.1 Metro應用程序中,我嘗試將一組形狀從我的視圖模型綁定到MainPage.xaml中。每個形狀將有一個Left,TopPathData這將是一個RectangleGeometry包含矩形,我想畫在畫布內相應的位置。如何綁定Canvas中的形狀/控件的絕對座標

這是XAML:

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

數據上下文設置並正常工作。我從MainViewModel填充形狀和矩形確實出現在屏幕上,但問題是我不能讓矩形被放置在Canvas內的確切的LeftTop位置,即它們只被放置在(0,0 )。

我嘗試都結合PathCanvas.LeftCanvas.Top(明顯的方法我試過)以及與Style(I從WPF例如發現的方法)設置一個ItemContainerStyle是應該這樣做。但這些都沒有工作(我在xaml中添加了兩種方法供參考)。

那麼我做錯了什麼,我該如何讓矩形出現在相應的位置呢?


編輯:我的問題是一樣的,只是我針對的窗戶地鐵/ UWP爲此接受了答案不起作用 this one for WPF

回答

2

而是通過綁定到Transform來解決問題。

​​