我有一个显示图像的按钮:
<Button Command="{Binding Model.ZoomOnImage}">
<Image Source="{Binding Model.ImageSource}" Stretch="Uniform" />
</Button>
在Model类中,我有以下命令:
private ICommand _zoomOnImage;
public ICommand ZoomOnImage
{
get
{
if (_zoomOnImage == null)
_zoomOnImage = new RelayCommand(Zoom, CanZoom);
return _zoomOnImage;
}
}
private void Zoom() {...}
private bool CanZoom() {...}
当用户单击按钮时,我想获得单击[X; Y]坐标(执行Zoom(int X, int Y)
)。如何修改我的代码来实现这一目标?
答案 0 :(得分:1)
使用自定义按钮或根据需要创建行为。还将属性添加到您的视图模型public Point MousePosition{get;set;}
<local:CustomButton Command="{Binding Model.ZoomOnImage}" MousePosition="{Binding MousePosition}">
<Image Source="{Binding Model.ImageSource}" Stretch="Uniform" />
</local:CustomButton>
public class CustomButton : Button
{
public Point MousePosition
{
get { return (Point)GetValue(MousePositionProperty); }
set { SetValue(MousePositionProperty, value); }
}
// Using a DependencyProperty as the backing store for MousePosition. This enables animation, styling, binding, etc...
public static readonly DependencyProperty MousePositionProperty = DependencyProperty.Register("MousePosition", typeof(Point), typeof(CustomButton), new FrameworkPropertyMetadata(new Point(), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
protected override void OnClick()
{
base.OnClick();
MousePosition = Mouse.GetPosition(this);
}
}