2011-02-16 56 views
2

我的应用程序是沿着一个标准的兵图示例,在那里我基本上配售对象在规定的X,在画布Y坐标和渲染他们的线。作为我的画布上的Bing背景,我实际上有一堆线/路径指令来绘制底层内容,并且需要放置的对象在用户平移/缩放时“粘”到该底层内容。我使用MVVM框架(特别是MVVM光),并希望最终的解决方案是MVVM友好的,但现在需要解决的我不物体粘在底层的“地图”,当用户平移/翻译的问题。MVVM友好的方式来平移/缩放手势

我的主视图是这样的:在适当的所需X的各类LocationViewModels(RestaurantViewModel,ParkingGarageViewModel,等..)来呈现每个位置的定义

<Canvas Margin="10" Background="AliceBlue" IsManipulationEnabled="True" ManipulationStarting="manipulatingStarting" ManipulationDelta="manipulationDelta"> 
    <view:MapView x:Name="viewContent"> 
     <view:MapView.RenderTransform> 
      <MatrixTransform/> 
     </vw:MapView.RenderTransform> 
    </view:MapView> 
    <ItemsControl ItemsSource="{Binding Locations}"/> 
</Canvas> 

和我有不同的数据模板,y位置使用他们的看法。

现在,耽搁了主视图代码如下:

private void manipulatingStarting(object sender, System.Windows.Input.ManipulationStartingEventArgs e) 
{ 
    // just let the user pan/translate for now 
    e.Mode = System.Windows.Input.ManipulationModes.Translate; 
} 

private void manipulationDelta(object sender, System.Windows.Input.ManipulationDeltaEventArgs e) 
{ 
    MatrixTransform transform = viewContent.RenderTransform as MatrixTransform; 
    if (transform != null) 
    { 
     Matrix matrix = transform.Matrix; 
     matrix.Translate(e.DeltaManipulation.Translation.X,  e.DeltaManipulation.Translation.Y); 
     transform.Matrix = matrix; 
     e.Handled = true; 
    } 
} 

这允许用户平移/翻译的MapView内容,但是从ItemsControl的绑定到画布的对象不动与用户的手势。我是否需要以某种方式连接操纵事件以更新ItemsControl中每个项目的相对(x,y)?

回答

0

试着这么做:

<map:Map CredentialsProvider="xyz" 
    NavigationVisibility="Collapsed"> 
<map:MapLayer> 
    <map:MapItemsControl ItemTemplate="{StaticResource MyTemplate1}" 
         ItemsSource="{Binding Shops}" /> 
</map:MapLayer> 
<map:MapLayer> 
    <map:MapItemsControl ItemTemplate="{StaticResource MyTemplate2}" 
         ItemsSource="{Binding Theaters}" /> 
</map:MapLayer> 

和模板:

<DataTemplate x:Key="MyTemplate1"> 
<Grid map:MapLayer.Position="{Binding Location}" 
     map:MapLayer.PositionOrigin="Center" 
     RenderTransformOrigin="0.5,0.5"> 
    <rb:BindingHelper.Binding> 
     <rb:RelativeSourceBinding Path="ZoomLevel" 
            TargetProperty="RenderTransform" 
            RelativeMode="FindAncestor" 
            AncestorType="Microsoft.Maps.MapControl.Map" 
            Converter="{StaticResource PushpinScaleTransform}" /> 
    </rb:BindingHelper.Binding> 
    <Image Source="MyImage.png" /> 

然后在我的模板,我用RelativeSource绑定绑定到地图缩放级别和一个转换器来计算缩放。找到缩放转换器here,但最终使用了从here的缩放逻辑。