以分离方式

时间:2016-02-12 22:56:49

标签: c# wpf xaml mvvm

我有这样的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>

我在ViewModel中发生的所有业务逻辑都需要与此控件进行交互并使其完成任务。理想情况下,我希望View不知道它是什么类型的控件。我通过在XAML中创建一个条目,使用UserControls一直这样做:

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

然后ViewModel可以设置从ContentControl继承的“Menu Control”对象。

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

有没有办法可以放入标准<control/>并将地图控件分配给它?

基本上,我只想以最分离的方式与ViewModel中的Map对象进行交互。

2 个答案:

答案 0 :(得分:1)

如ContentControl类的Content属性描述所述:

  

因为Content属性是Object类型,所以没有   你可以放在ContentControl中的限制。内容是   由ContentPresenter显示,它位于ControlTemplate中   ContentControl。 WPF中的每个ContentControl类型都有一个   ContentPresenter的默认ControlTemplate

因此,您仍然可以像以前一样绑定您的Content属性:

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

答案 1 :(得分:0)

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

然后为您的控件创建一个模板,其中包含受

约束的ContentControl

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

我认为无论如何......我在手机上。 男人我爱Intellisense ..

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