2016-02-12 37 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