2016-02-12 38 views
0

我有XAML这样可以让我在第三方地图控件添加这发生在视图模型添加第三方控件到视图模型中解耦方法

<UserControl x:Class="AssemblyName.Views.CustomMapView" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:local="clr-namespace:AssemblyName.Views" 
      xmlns:ioc="clr-namespace:AssemblyName.Ioc" 
      xmlns:esri="http://schemas.esri.com/arcgis/runtime/2013" 
      mc:Ignorable="d" 
      d:DesignHeight="300" d:DesignWidth="300" 
      ioc:ViewModelLocator.AutoWireViewModel="True"> 
     <esri:MapView x:Name="customMapView"> 
      <esri:Map x:Name="customMap"> 
       <esri:ArcGISTiledMapServiceLayer ID="BaseMap" ServiceUri="http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer"/> 
      </esri:Map> 
     </esri:MapView> 

</UserControl> 

我所有的业务逻辑,需要与此控件交互并让它做事情。理想情况下,我希望视图不知道它是什么类型的控件。我做这一切与用户控件的时间通过在XAML中的条目,如:

<ContentControl Name="menuControl" Content="{Binding MenuControl}"/> 

然后视图模型可以设置任何“菜单控制”对象,自ContentControl继承。

因为customMapView不从ContentControl继承,我不能使用上面我通常使用的方法。它从Control继承。

有没有一种方法可以放入标准<control/>并将我的地图控制分配给它?

基本上,我只是想以最可能的解耦方式与ViewModel中的Map对象进行交互。

回答

1

由于在ContentControl中类的Content属性描述说:

因为内容属性是Object类型的,还有你可以把什么在ContentControl.The内容没有限制 是 显示由ContentPresenter提供,它位于ContentControl的 的ControlTemplate中。在WPF每ContentControl中类型有 ContentPresenter在其默认的ControlTemplate

所以,你仍然可以绑定你的内容属性喜欢你用来:

<ContentControl Content="{Binding MyDisplayedControl}"/> 
+0

那很简单,呃?我会在今晚晚些时候尝试这个 – matrixugly

+0

钉住它......这比我想象的要容易得多。谢谢! ContentControl ftw。 – matrixugly

+0

很高兴我能帮到你 –

0

为什么不自ContentControl继承?它显然有内容。

然后创建您的控制模板,其中包含一个ContentControl中谁是

<ContentControl Content="{Binding RelativeSource="{RelativeSource Mode=TemplatedParent}, Path=Content}"/>

我认为是正确的,无论如何...我在我的手机绑定。 我喜欢Intellisense的人..

无论如何,具体将内容控件的内容设置为另一个控件似乎是多余的。

+0

嘿谢谢。我喜欢莱昂内尔的方法。我只是做了'。原因是我的视图没有关于其ViewModel的概念,并且通过在ViewModel中指定ContentControl的内容,我可以自由地将视图控件及其所有业务逻辑与ViewModel中的其他内容完全交换。 – matrixugly