マウスで画像をトリミングしてサイズ変更できるアプリケーションをプログラミングしています。 MVVMでEvent Argsをどのように渡すことができたのか、問題があったので、MVVM Lightを試してみることにしました。私は本当に私は私のRelayCommandsが間違って実装されていることを前提としていたが、この問題を解決する方法がわからないMVVMライトでMouseEvent Argingを渡す
An unhandled exception of type 'System.InvalidCastException' occurred in GalaSoft.MvvmLight.Platform.dll
Additional information: Das Objekt des Typs "System.Windows.Point" kann nicht in Typ "System.Windows.Input.MouseEventArgs" umgewandelt werden.
:今毎回私のマウスを画像の上で、私はエラーを受け取ります。
私は次のようにXAMLでバインディングをした:
RelayCommand<MouseButtonEventArgs> mouseDownCommand;
public RelayCommand<MouseButtonEventArgs> MouseDownCommand
{
get
{
if (mouseDownCommand == null)
{
mouseDownCommand = new RelayCommand<MouseButtonEventArgs>(MouseDown);
}
return mouseDownCommand;
}
}
public void MouseDown(MouseButtonEventArgs e)
{
this.currentImage.StartPoint = e.GetPosition(this.currentImage.CnvImage);
}
RelayCommand<System.Windows.Input.MouseEventArgs> mouseMoveCommand;
public RelayCommand<System.Windows.Input.MouseEventArgs> MouseMoveCommand
{
get
{
if (mouseMoveCommand == null)
{
mouseMoveCommand = new RelayCommand<System.Windows.Input.MouseEventArgs>(MouseMove);
}
return mouseMoveCommand;
}
}
public void MouseMove(System.Windows.Input.MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
var pos = e.GetPosition(this.currentImage.CnvImage);
var x = Math.Min(pos.X, this.currentImage.StartPoint.X);
var y = Math.Min(pos.Y, this.currentImage.StartPoint.Y);
var w = Math.Max(pos.X, this.currentImage.StartPoint.X) - x;
var h = Math.Max(pos.Y, this.currentImage.StartPoint.Y) - y;
var rect = new System.Windows.Shapes.Rectangle
{
Stroke = System.Windows.Media.Brushes.LightBlue,
StrokeThickness = 2
};
this.currentImage.RectSelectArea = rect;
this.currentImage.RectSelectArea.Width = w;
this.currentImage.RectSelectArea.Height = h;
Canvas.SetLeft(this.currentImage.RectSelectArea, x);
Canvas.SetTop(this.currentImage.RectSelectArea, y);
}
}
RelayCommand<MouseButtonEventArgs> mouseUpCommand;
public RelayCommand<MouseButtonEventArgs> MouseUpCommand
{
get
{
if (mouseUpCommand == null)
{
mouseUpCommand = new RelayCommand<MouseButtonEventArgs>(MouseUp);
}
return mouseUpCommand;
}
}
public void MouseUp(MouseButtonEventArgs e)
{
System.Windows.Controls.Image croppedImage = new System.Windows.Controls.Image();
croppedImage.Width = 100;
croppedImage.Margin = new Thickness(5);
this.currentImage.CropXPosition = (int)this.currentImage.StartPoint.X;
this.currentImage.CropYPosition = (int)this.currentImage.StartPoint.Y;
this.currentImage.CropWidth = (int)this.currentImage.RectSelectArea.Width;
this.currentImage.CropHeight = (int)this.currentImage.RectSelectArea.Height;
CroppedBitmap cb = new CroppedBitmap(
(BitmapSource)this.currentImage.ImagePath, new Int32Rect(
this.currentImage.CropXPosition, this.currentImage.CropYPosition, this.currentImage.CropWidth, this.currentImage.CropHeight));
croppedImage.Source = cb;
}
MouseButtonEventToArgsToPointConverter::私のViewModelには、このようになります
<Image x:Name="_image" Margin="10" Source="{Binding CurrentImage.ImagePath, UpdateSourceTrigger=PropertyChanged}"
HorizontalAlignment="Left" VerticalAlignment="Top" Stretch="Fill" MaxHeight="300">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDown">
<cmd:EventToCommand Command="{Binding Mode=OneWay, Path=MouseDownCommand}"
EventArgsConverter="{StaticResource MouseButtonEventArgsToPointConverter}"
EventArgsConverterParameter="{Binding ElementName=_image}"
PassEventArgsToCommand="True" />
</i:EventTrigger>
<i:EventTrigger EventName="MouseMove">
<cmd:EventToCommand Command="{Binding Mode=OneWay, Path=MouseMoveCommand}"
EventArgsConverter="{StaticResource MouseEventArgsToPointConverter}"
EventArgsConverterParameter="{Binding ElementName=_image}"
PassEventArgsToCommand="True" />
</i:EventTrigger>
<i:EventTrigger EventName="MouseUp">
<cmd:EventToCommand Command="{Binding Mode=OneWay, Path=MouseUpCommand}"
EventArgsConverter="{StaticResource MouseButtonEventArgsToPointConverter}"
EventArgsConverterParameter="{Binding ElementName=_image}"
PassEventArgsToCommand="True" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Image>
class MouseButtonEventArgsToPointConverter : IEventArgsConverter
{
public object Convert(object value, object parameter)
{
var args = (MouseButtonEventArgs)value;
var element = (FrameworkElement)parameter;
var point = args.GetPosition(element);
return point;
}
}
私もからthisアドバイスに従っ同じ質問ではない場合は同様の質問ですが、RelayCommand wタイプポイントとして動作しません。 これを修正するにはどうすればよいですか?または私のアプローチは間違っていますか?どんな種類の助けや提案にも本当に感謝しています。
MouseButtonEventArgsToPointConverter - このコードを投稿できますか? –
@TimRutter編集された投稿のコードを参照してください –
あなたはUIロジックをビューモデルに詰め込んでいます。 MVVM!=コードビハインドなし。 MVVMはビジネスロジックとUIロジックを分離します。あなたのUIロジックはビューのコードビハインドに属します。あなたがやっていることは、このダンスであなた自身のことをもっと難しくすることです。 – Will