从转换器获取值到视图模型

问题描述 投票:0回答:2

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

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,但它是alwaws (0,0),如何将转换器类的点值传递给视图模型类?

c# wpf xaml mvvm-light
2个回答
1
投票

你应该做下一个:

  1. 将网格背景设置为不透明的内容。 wpf中的透明对象不会影响鼠标事件。 <Grid Background="#01FFFFFF">
  2. 使用命令的参数创建方法 public ICommand DoSomething { get; private set; } public TestVM() { DoSomething = new RelayCommand<Point>(SomeAction); } private void SomeAction(Point point) { }
  3. 只保存一个触发器,使其看起来像那个:

 <i:Interaction.Triggers>
<i:EventTrigger EventName="MouseMove">
                <cmd:EventToCommand
             Command="{Binding DoSomething}"
             EventArgsConverter="{StaticResource mConverter}"
             PassEventArgsToCommand="True" />
            </i:EventTrigger>
        </i:Interaction.Triggers>

1
投票

我刚刚使用了xaml的CallMethodAction。值得注意的是,我正在使用新的行为NuGet包,其中描述了in this blog post

这是我的xaml:

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

这是我的ViewModel:

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

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

© www.soinside.com 2019 - 2024. All rights reserved.