代码之家  ›  专栏  ›  技术社区  ›  A.Pissicat

单击按钮[复制]时获取鼠标坐标

  •  0
  • A.Pissicat  · 技术社区  · 7 年前

    <Button Command="{Binding Model.ZoomOnImage}">
        <Image Source="{Binding Model.ImageSource}" Stretch="Uniform" />
    </Button>
    

    在我的模型类中,我有以下命令:

    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) ). 如何修改代码来实现这一点?

    1 回复  |  直到 7 年前
        1
  •  1
  •   A.Pissicat    7 年前

    如果需要,可以使用自定义按钮或创建行为。还可以将属性添加到viewmodel中 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);
        }
    }