c# - 从转换器获取值到视图模型
问题描述
我的目标是在鼠标移过给定网格时获取鼠标点位置。
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)
,如何将转换器类的点值传递给视图模型类?
解决方案
我刚从CallMethodAction
xaml 使用过。值得注意的是,我正在使用此博客文章中描述的新行为 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);
}
}
这确实需要您知道传递了哪种类型的事件参数,否则您将收到方法签名匹配错误。
推荐阅读
- javascript - 如何将 var 值从一个函数移动到另一个函数?Javascript
- arrays - 从 jsonb 列的 json 属性中的数组中获取数组元素
- entity-framework - 我无法在 .NET Core 3.1.9 中使用实体框架创建新的 Web API
- r - 正态混合分布
- python - 在 Python 中以递归方式返回由列表表示的树的节点
- cryptography - 保存使用用户密码加密的私钥是否比在数据库中存储哈希更安全?
- pandas - 根据其他列中的条件替换值
- directx - 为什么每次 OpenSharedResource 调用都会导致显存增加(泄漏)?
- r - 创建 df 时:对于一列,仅在特定行中添加值
- c - 如何使用纯 C 的指针设计函数