0
在Windows 8.1 Metro应用程序中,我尝试将一组形状从我的视图模型绑定到MainPage.xaml中。每个形状将有一个Left
,Top
和PathData
这将是一个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
内的确切的Left
和Top
位置,即它们只被放置在(0,0 )。
我尝试都结合Path
的Canvas.Left
和Canvas.Top
(明显的方法我试过)以及与Style
(I从WPF例如发现的方法)设置一个ItemContainerStyle
是应该这样做。但这些都没有工作(我在xaml中添加了两种方法供参考)。
那么我做错了什么,我该如何让矩形出现在相应的位置呢?
编辑:我的问题是一样的,只是我针对的窗户地铁/ UWP为此接受了答案不起作用 this one for WPF。