MVVM Light - 带有Pushpins的Relaycommand

Jas*_*Jas 2 silverlight bing-maps pushpin mvvm-light

我是将一些图钉绑定到MapLayer的数据.它们显示正常,但是当我使用relay命令从鼠标leftbuttonUp传递eventargs时,对象源是一个elipse.我在MapPolygon上使用了这个方法,并从对象中获取了我想要的信息.

因为我是mvvm的新手,也许我正在接受这一点,所以请让我知道!

这适用于我的MapPolygons(vm引用我的extendedMapPolygon类的命名空间)

    <DataTemplate x:Name="polyTemplate">
        <vm:extendedMapPolygon cName="{Binding _cName}" Locations="{Binding _Locations}" />
    </DataTemplate>
Run Code Online (Sandbox Code Playgroud)

这是MapLayer中的XAML

    <m:MapItemsControl ItemTemplate="{StaticResource polyTemplate}" ItemsSource="{Binding  Path=_PolyforDisplay, Mode=TwoWay}"  >
        <i:Interaction.Triggers>
               <i:EventTrigger EventName="MouseLeftButtonUp">
                    <cmd:EventToCommand Command="{Binding Path=PolySelCommand}" PassEventArgsToCommand="True" ></cmd:EventToCommand>
               </i:EventTrigger>
        </i:Interaction.Triggers>
    </m:MapItemsControl>
Run Code Online (Sandbox Code Playgroud)

在我的viewModel构造函数中

PolySelCommand = new RelayCommand<MouseButtonEventArgs>(PolySelCommandExecute);
Run Code Online (Sandbox Code Playgroud)

最后是实际的命令

        public RelayCommand<MouseButtonEventArgs> PolySelCommand { get; set; }
    private void PolySelCommandExecute(MouseButtonEventArgs cmp)
    {
        Polygon poly = cmp.OriginalSource as Polygon;
        extendedMapPolygon ePoly = poly.Parent as extendedMapPolygon;
        _MapPolygonSelected =  ePoly.cName;
    }
Run Code Online (Sandbox Code Playgroud)

(我把它放在这里,以显示我目前使用的方法,并希望它可能对其他人有用!)

然而,当我用Pushpin尝试相同的东西时,cmp.OriginalSource是一个椭圆,我似乎无法得到任何其他东西.

我的Pushpin代码(我只是在这段代码中使用MapControl中的Pushpins)

    <DataTemplate x:Name="ppTemplate">
        <m:Pushpin ToolTipService.ToolTip="{Binding _psName}" Location="{Binding _Location}" />
    </DataTemplate>

    <m:MapItemsControl ItemTemplate="{StaticResource ppTemplate}" ItemsSource="{Binding Path=_PinsforDisplay, Mode=TwoWay}">
        <i:Interaction.Triggers>
             <i:EventTrigger EventName="MouseLeftButtonUp">
                  <cmd:EventToCommand Command="{Binding Path=pinSelCommand}" PassEventArgsToCommand="True" ></cmd:EventToCommand>
             </i:EventTrigger>
        </i:Interaction.Triggers>
    </m:MapItemsControl>
Run Code Online (Sandbox Code Playgroud)

我应该使用命令参数吗?或者当我点击图钉时,另一种将文本传递到我的viewmodel的方法,这是我真正想要的.

sch*_*mbo 5

我将触发器移动到图钉元件.

    <DataTemplate x:Name="ppTemplate">
        <m:Pushpin ToolTipService.ToolTip="{Binding _psName}" Location="{Binding _Location}">
             <i:Interaction.Triggers>
                    <i:EventTrigger EventName="MouseEnter">
                        <i:Interaction.Triggers>
                            <i:EventTrigger EventName="MouseLeftButtonUp">
                                <cmd:EventToCommand Command="{Binding Path=DataContext.pinSelCommand}" 
                                                    CommandParameter="{Binding WhateverPropertyHasTheText" />
                            </i:EventTrigger>
                        </i:Interaction.Triggers>
                    </i:EventTrigger>
                </i:Interaction.Triggers>
        </m:Pushpin>
    </DataTemplate>
Run Code Online (Sandbox Code Playgroud)

请注意,我将作为命令参数传递给您想要从_PinsForDisplay中的对象发送的任何属性.此外,命令的绑定稍有改变,因为绑定与datatemplate内部不同.

然后你必须将viewmodel上的RelayCommand更改为RelayCommand.

我没有测试这段代码,但是非常相似的东西对我有用,所以希望它可以引导你找到解决方案.

  • 我必须添加ElementName = LayoutRoot才能使其正常工作 - 但确实如此!谢谢你的帮助.**Command ="{Binding DataContext.pinSelCommand,ElementName = LayoutRoot}"** (4认同)