首页 > 解决方案 > 从转换器获取值到视图模型

问题描述

我的目标是在鼠标移过给定网格时获取鼠标点位置。

XAML:

<UserControl.Resources>     
  <helpers:MouseButtonEventArgsToPointConverter x:Key="mConverter"/>
</UserControl.Resources>
...
<Grid DataContext="{Binding CartesianChartVM, Source={StaticResource Locator}}">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="MouseMove">
                    <i:InvokeCommandAction Command="{Binding MouseMoveCommand}"   CommandParameter="ThePoint"/>
                </i:EventTrigger>
                <i:EventTrigger EventName="MouseMove">
                    <cmd:EventToCommand
             Command="{Binding Main.MouseMoveCommand, Mode=OneWay}"
             EventArgsConverter="{StaticResource mConverter}"
             EventArgsConverterParameter="{Binding ElementName=ThePoint}"
             PassEventArgsToCommand="True" />
                </i:EventTrigger>
            </i:Interaction.Triggers>
</Grid>

在哪里:

xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Platform"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"

转换器:

public class MouseButtonEventArgsToPointConverter : IEventArgsConverter
{
    public object Convert(object value, object parameter)
    {
        var args = (MouseEventArgs)value;
        var element = (FrameworkElement)parameter;
        var point = args.GetPosition(element);
        return point;
    }
}

视图模型:

    public ICommand MouseMoveCommand { get; private set; }
    private Point thePoint;

    public CartesianChartVM()
    {
        MouseMoveCommand = new RelayCommand(MouseMoveMethod);
    }
    void MouseMoveMethod()
    {
        Point p= ThePoint;
    }

    public Point ThePoint
    {
        get { return thePoint; }
        set
        {
            thePoint = value;
            RaisePropertyChanged("ThePoint");
        }
    }

如果我在转换器类中放置断点,我可以看到点坐标,但是当 mousemove 事件发生时,如何将这个点值传递给视图模型。

我在视图模型内部尝试过,Point ThePoint但总是这样(0,0),如何将转换器类的点值传递给视图模型类?

标签: c#wpfxamlmvvm-light

解决方案


我刚从CallMethodActionxaml 使用过。值得注意的是,我正在使用此博客文章中描述的新行为 NuGet 包

这是我的xml:

<Window x:Class="mousepointerTest.MainWindow"
    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"
    xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
    xmlns:local="clr-namespace:mousepointerTest"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800"
    >
<Window.DataContext>
    <local:MainWindowViewModel />
</Window.DataContext>
<Grid>
    <DataGrid x:Name="TestGrid" 
              HorizontalAlignment="Stretch"
              VerticalAlignment="Stretch"
              BorderBrush="Black"
              BorderThickness="3">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="MouseMove">
                <i:CallMethodAction MethodName="OnMouseMove"
                                    TargetObject="{Binding}" />
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </DataGrid>
</Grid>

这是我的视图模型:

public class MainWindowViewModel
{
    public void OnMouseMove(object sender, MouseEventArgs e)
    {
        var point = e.GetPosition((IInputElement)e.Source);
    }
}

这确实需要您知道传递了哪种类型的事件参数,否则您将收到方法签名匹配错误。


推荐阅读