将UserControl的内容值传输到其中的Control

时间:2010-12-18 09:58:02

标签: silverlight mvvm bing-maps

我需要将bing maps控件扩展为更多用户MVVM友好(具体而言,ZoomLevel和BoundingRect属性不是依赖属性)。我将控件包装在自定义用户控件中(我还需要添加元素以进行其他地图选择,例如谷歌地图)。我需要将UserControl的内容值传输到BingMapsControl:

 

<UserControl x:Class="RevOptWebControls.MVVMMapControl" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d" 
    xmlns:m="clr-namespace:Microsoft.Maps.MapControl;assembly=Microsoft.Maps.MapControl" 
    xmlns:mCore="clr-namespace:Microsoft.Maps.MapControl.Core;assembly=Microsoft.Maps.MapControl"              
    d:DesignHeight="300" d:DesignWidth="400" 
    x:Name="Root"> 

    <Grid x:Name="LayoutRoot" Background="White"> 
        <m:Map CredentialsProvider="Al_H1LepP6chseYMu31RK76El6k4SUkx2KVrxeqobE3rTXooFPieuEJ6qiuA211I" 
               CopyrightVisibility="Collapsed"  
               LogoVisibility="Collapsed" 
               ScaleVisibility="Visible" 
               NavigationVisibility="Visible" 
               x:Name="MyMap"> 
        </m:Map> 
        <ComboBox x:Name="c_MapTypes" HorizontalAlignment="Right" VerticalAlignment="Top" SelectedIndex="0" Height="30" SelectionChanged="MapTypes_SelectionChanged"> 
            <ComboBoxItem>Google Roads</ComboBoxItem> 
            <ComboBoxItem>Google Aerial</ComboBoxItem> 
            <ComboBoxItem>Bing Maps Roads</ComboBoxItem> 
            <ComboBoxItem>Bing Maps Aerial</ComboBoxItem> 
            <ComboBoxItem>Open Street Maps</ComboBoxItem> 
            <ComboBoxItem>Yahoo Street</ComboBoxItem> 
            <ComboBoxItem>Yahoo Aerial</ComboBoxItem> 
            <ComboBoxItem>Blank Map</ComboBoxItem> 
        </ComboBox> 
    </Grid> 
</UserControl>

更新:弄清楚如何做到这一点。共享控件源代码:http://basaratali.blogspot.com/2010/12/mvvm-version-of-bing-maps-with-google.html

1 个答案:

答案 0 :(得分:1)

为什么不尝试使用自定义模板Custom Control。使用

`{TemplateBinding Content}` 

ContentMap控件绑定。

实施例

<Style TargetType="local:MVVMMapControl">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="local:MVVMMapControl">
                <Grid x:Name="LayoutRoot"
                      Background="White">
                    <m:Map CredentialsProvider="Al_H1LepP6chseYMu31RK76El6k4SUkx2KVrxeqobE3rTXooFPieuEJ6qiuA211I"
                           CopyrightVisibility="Collapsed"
                           LogoVisibility="Collapsed"
                           ScaleVisibility="Visible"
                           NavigationVisibility="Visible"
                           x:Name="MyMap"
                           Content="{TemplateBinding ContentControl.Content}"></m:Map>
                    <ComboBox x:Name="c_MapTypes"
                              HorizontalAlignment="Right"
                              VerticalAlignment="Top"
                              SelectedIndex="0"
                              Height="30"
                              SelectionChanged="MapTypes_SelectionChanged">
                        <ComboBoxItem>Google Roads</ComboBoxItem>
                        <ComboBoxItem>Google Aerial</ComboBoxItem>
                        <ComboBoxItem>Bing Maps Roads</ComboBoxItem>
                        <ComboBoxItem>Bing Maps Aerial</ComboBoxItem>
                        <ComboBoxItem>Open Street Maps</ComboBoxItem>
                        <ComboBoxItem>Yahoo Street</ComboBoxItem>
                        <ComboBoxItem>Yahoo Aerial</ComboBoxItem>
                        <ComboBoxItem>Blank Map</ComboBoxItem>
                    </ComboBox>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

类别:

public class MVVMMapControl : ContentControl // Notice this inherits from ContentControl for its Content Property
{
    public MVVMMapControl()
    {
        this.DefaultStyleKey = typeof(MVVMMapControl);
    }
}