【问题标题】:MVVM Light - Relaycommand with PushpinsMVVM Light - 带有图钉的继电器命令
【发布时间】:2011-05-18 09:28:58
【问题描述】:

我正在将一些图钉数据绑定到 MapLayer。它们显示得很好,但是当我使用中继命令从鼠标左键向上传递事件参数时,对象源是一个椭圆。我在 MapPolygon 上使用了这种方法,并从对象中获取了我想要的信息。

由于我是 mvvm 的新手,所以可能我的态度很差,所以请告诉我!

这适用于我的 MapPolygons(vm 引用了我的扩展 MapPolygon 类的命名空间)

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

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

在我的 viewModel 构造函数中

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

(我把它放在这里是为了展示我目前使用的方法,并希望它对其他人有用!)

但是,当我用图钉尝试同样的事情时,cmp.OriginalSource 是一个椭圆,我似乎无法通过其他任何东西。

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

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

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

【问题讨论】:

  • 您试图将什么文本传递给视图模型上的命令?它是“_PinsforDisplay”中对象的属性吗?
  • 是 - 属性中保存的一些文本

标签: silverlight mvvm-light bing-maps pushpin


【解决方案1】:

我会将触发器移动到图钉元素。

    <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 中的对象发送的任何属性作为命令参数传递。此外,命令的绑定略有变化,因为绑定与数据模板内部不同。

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

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

【讨论】:

  • 感谢您的回复 - 我将在接下来的几天内尝试一下并回复
  • 很抱歉我迟迟没有回复 - 时间已经过去了。我似乎无法让这个工作。我收到此错误向“System.Windows.Interactivity.TriggerCollection”类型的集合添加值引发异常无论我如何尝试在数据模板中添加触发器。我已将我的代码精简到最低限度,但仍然没有乐趣 - 有什么想法吗?感谢您的帮助
  • 我必须添加 ElementName=LayoutRoot 才能让它工作 - 但它确实做到了!谢谢你的帮助。 Command="{Binding DataContext.pinSelCommand, ElementName=LayoutRoot}"
猜你喜欢
  • 1970-01-01
  • 2015-02-26
  • 2022-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多