从转换器获取值到视图模型
Get the value from the converter to the view model
我的目标是在鼠标移过给定网格时获取鼠标点位置。
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");
}
}
If I put breakpoint inside the converter class I can see the point coordinates, but how can I get this point value to the view model when mousemove event occures.
I tried with Point ThePoint
inside the view model but it's alwaws (0,0)
, How do I pass the point value fom the converter class to the view model class?
我刚刚使用了 xaml 中的 CallMethodAction
。值得注意的是,我正在使用 in this blog post
中描述的新行为 NuGet 包
这是我的 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);
}
}
这确实要求您知道传递的事件参数类型,否则您将收到方法签名匹配错误。
你接下来应该做的:
将网格背景设置为不透明。 wpf 中的透明对象不会缓存鼠标事件。
<Grid Background="#01FFFFFF">
为您的命令创建带有参数的方法
public ICommand DoSomething { get; private set; }
public TestVM()
{
DoSomething = new RelayCommand<Point>(SomeAction);
}
private void SomeAction(Point point)
{
}
只保存一个触发器并使其看起来像那个:
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseMove">
<cmd:EventToCommand
Command="{Binding DoSomething}"
EventArgsConverter="{StaticResource mConverter}"
PassEventArgsToCommand="True" />
</i:EventTrigger>
</i:Interaction.Triggers>
我的目标是在鼠标移过给定网格时获取鼠标点位置。
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");
}
}
If I put breakpoint inside the converter class I can see the point coordinates, but how can I get this point value to the view model when mousemove event occures.
I tried with
Point ThePoint
inside the view model but it's alwaws(0,0)
, How do I pass the point value fom the converter class to the view model class?
我刚刚使用了 xaml 中的 CallMethodAction
。值得注意的是,我正在使用 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);
}
}
这确实要求您知道传递的事件参数类型,否则您将收到方法签名匹配错误。
你接下来应该做的:
将网格背景设置为不透明。 wpf 中的透明对象不会缓存鼠标事件。
<Grid Background="#01FFFFFF">
为您的命令创建带有参数的方法
public ICommand DoSomething { get; private set; } public TestVM() { DoSomething = new RelayCommand<Point>(SomeAction); } private void SomeAction(Point point) { }
只保存一个触发器并使其看起来像那个:
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseMove">
<cmd:EventToCommand
Command="{Binding DoSomething}"
EventArgsConverter="{StaticResource mConverter}"
PassEventArgsToCommand="True" />
</i:EventTrigger>
</i:Interaction.Triggers>