我有一个 Thumb 控件,用户应该能够在 UI 上拖动它。当我尝试拖动控件时,出现异常“找不到方法 Thumb_DragDelta 的目标”。我认为问题可能在于我在视图模型中编写方法的方式,但我不确定。此外,该方法不是很好的 MVVM,因为它直接引用视图,但同样不知道如何使它更好。我对 Caliburn Micro 和一般编程非常陌生。任何帮助表示赞赏!
视图中的拇指控件:
<Thumb cal:Message.Attach="[Event DragDelta] = [Action Thumb_DragDelta($view)]"
Background="blue"
HorizontalAlignment="Left"
Width="20"/>
以及viewmodel中对应的方法:
private Thumb _thumb;
public Thumb MyThumb
{
get
{
return _thumb;
}
set {
_thumb = value;
NotifyOfPropertyChange(() => MyThumb);
}
}
public void Thumb_DragDelta(Views.NaturalDisasterView view, DragDeltaEventArgs e)
{
try
{
var transform = (TranslateTransform)MyThumb.RenderTransform;
transform.X = Math.Max(0, Math.Min(transform.X + e.HorizontalChange, view.ActualWidth - MyThumb.ActualWidth));
view.AfterScene.Clip = new RectangleGeometry() { Rect = new Rect(0, 0, transform.X, view.ActualHeight) };
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
}