我正在尝试使用鼠标右键来移动图像,方法是使用MatrixTransform
(由于要缩放图像,所以我正在使用MatrixTransform
)。
这是我的Xaml
代码:
<Image x:Name="StreamImage" Source="{Binding Image, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Stretch="None"
RenderTransform="{Binding Overlays.RenderTransform, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
<ItemsControl x:Name="StreamCanvasItemSource" ItemsSource="{Binding CanvasItems, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="{Binding ActualWidth, ElementName=StreamImage}" Height="{Binding ActualHeight, ElementName=StreamImage}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas x:Name="StreamCanvas" Background="Transparent" Width="{Binding ActualWidth, ElementName=StreamImage}"
Height="{Binding ActualHeight, ElementName=StreamImage}"
Visibility="{Binding ImageExists, Converter={StaticResource BooleanToVisibilityConverter}}">
<Interactivity:Interaction.Triggers>
<Interactivity:EventTrigger EventName="PreviewMouseWheel">
<Command:EventToCommand Command="{Binding Overlays.OnMouseWheelCommand}" PassEventArgsToCommand="True" />
</Interactivity:EventTrigger>
<Interactivity:EventTrigger EventName="MouseMove">
<Command:EventToCommand Command="{Binding Overlays.OnMouseMoveCommand}" PassEventArgsToCommand="True"
CommandParameter="{Binding ElementName=StreamImage}" />
</Interactivity:EventTrigger>
<Interactivity:EventTrigger EventName="MouseDown">
<Command:EventToCommand Command="{Binding OnMouseDownCommand}" PassEventArgsToCommand="True" />
</Interactivity:EventTrigger>
<Interactivity:EventTrigger EventName="MouseEnter">
<Command:EventToCommand Command="{Binding Overlays.OnEnterExitCommand}" CommandParameter="Enter" />
</Interactivity:EventTrigger>
<Interactivity:EventTrigger EventName="MouseLeave">
<Command:EventToCommand Command="{Binding Overlays.OnEnterExitCommand}" CommandParameter="Leave" />
</Interactivity:EventTrigger>
</Interactivity:Interaction.Triggers>
</Canvas>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
在我的ViewModel
中:
private MatrixTransform m_renderTransform = new MatrixTransform();
private Point m_previouseLocation = new Point(-1, -1);
public Point MouseLoaction { get; set; }
public MatrixTransform RenderTransform
{
get => m_renderTransform;
set
{
m_renderTransform = value;
OnPropertyChanged();
}
}
private void OnMouseMove(object parameter)
{
MouseLoaction = Mouse.GetPosition(image);
if ((m_previouseLocation.X == -1) && (m_previouseLocation.Y == -1))
m_previouseLocation = MouseLoaction;
if (Mouse.RightButton == MouseButtonState.Pressed)
{
var matrix = RenderTransform.Matrix;
matrix.TranslatePrepend(MouseLoaction.X - m_previouseLocation.X, MouseLoaction.Y - m_previouseLocation.Y);
RenderTransform.Matrix = matrix;
}
m_previouseLocation = MouseLoaction;
}
}
当我执行以下操作时,图像确实会移动(平移),但非常模糊,就像图像在移动时“颠簸”一样。
我也尝试使用TransformGroup
,将MatrixTransform
作为缩放的第一个孩子,将TranslateTransform
作为移动的第二个孩子,但是当我进行了小动作。
我想念什么?