MVVM Light-带图钉的Relaycommand [英] MVVM Light - Relaycommand with Pushpins

查看:97
本文介绍了MVVM Light-带图钉的Relaycommand的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am data binding some pushpins to a MapLayer. They display fine, but when I use the relaycommand to pass the eventargs from the mouse leftbuttonUp, the object sources is an elipse. I have used this method on a MapPolygon and picked up the information I wanted from the object.

由于我是mvvm的新手,所以我可能对此表示不满,所以请告诉我!

Maybe I am aproaching this badly as I am new to mvvm, so please let me know!

这适用于我的MapPolygons(vm引用了我的ExtendedMapPolygon类的名称空间)

This works for my MapPolygons (vm references the namespace of my extendedMapPolygon class)

    <DataTemplate x:Name="polyTemplate">
        <vm:extendedMapPolygon cName="{Binding _cName}" Locations="{Binding _Locations}" />
    </DataTemplate>

这是MapLayer中的XAML

Here is the XAML in the MapLayer

    <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>

在我的viewModel构造函数中

In my viewModel constructor

PolySelCommand = new RelayCommand<MouseButtonEventArgs>(PolySelCommandExecute);

最后是实际命令

        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;
    }

(我将其放在此处以显示我当前正在使用的方法,并希望它可能对其他人有用!)

(I put this here to show both the method I am currently using and in the hope it may be of some use to others!)

但是,当我使用图钉尝试相同的操作时,cmp.OriginalSource是一个椭圆形,我似乎无法通过任何其他内容.

When I try the same thing with a Pushpin however, the cmp.OriginalSource is an ellipse and I can not seem to get anything else passed through.

我的图钉代码(我只是在此代码中使用MapControl中的图钉)

My Pushpin code (I am just using the Pushpins in the MapControl in this code)

    <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>

我应该使用命令参数吗?或当我单击图钉时将文本传递到我的视图模型的其他方法,这正是我真正想要的.

Should I be using the command parameter? Or someother way of passing text through to my viewmodel when I click on the pushpin, which is what I actually want.

推荐答案

我将触发器移到图钉元素.

I'd move the trigger to the pushpin element.

    <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>

注意,我要从_PinsForDisplay对象中发送要发送的任何属性作为命令参数.另外,该命令的绑定稍有更改,因为该绑定与数据模板内部的绑定不同.

Notice I'm passing as a command parameter whatever property you want to send from the objects in _PinsForDisplay. Also, the binding for the command changed slightly, as the binding is different from inside a datatemplate.

然后必须将视图模型上的RelayCommand更改为RelayCommand.

And then you'd have to change your RelayCommand on the viewmodel to RelayCommand.

我没有测试这段代码,但是非常相似的东西正在为我工​​作,因此希望它可以引导您找到解决方案.

I didn't test this code, but something very similar is working for me so hopefully it can lead you toward a solution.

这篇关于MVVM Light-带图钉的Relaycommand的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆