【问题标题】:Zoom in and out an image on mouse click using MVVM使用 MVVM 通过鼠标单击放大和缩小图像
【发布时间】:2021-02-11 12:48:49
【问题描述】:

在此应用程序中,我使用 WPF 中的 MVVM 使用按钮来放大和缩小图像。单击放大按钮时,高度和宽度会增加特定的量,反之亦然。但我想用鼠标双击来达到同样的效果。有什么方法可以使用 MVVM 实现相同的效果吗?

private ICommand zoomOutCommand;
public ICommand ZoomOutCommand {
    get {
        if (zoomOutCommand == null) {
            zoomOutCommand = new RelayCommand < object > (ZoomOutExecute, OutReturnBool, false);
        }
        return zoomOutCommand;
    }
}
private ICommand zoomInCommand;
public ICommand ZoomInCommand {
    get {
        if (zoomInCommand == null) {
            zoomInCommand = new RelayCommand < object > (ZoomInExecute, InReturnBool, false);
        }
        return zoomInCommand;
    }
}

private void ZoomInExecute(object obj) {
    //  Scale += stepScale;
    Height *= 1.2;
    Width *= 1.2;
}

private bool InReturnBool(object obj) {
    if (Height > 6 * iniWidth) return false;
    else return true;
}

private bool OutReturnBool(object obj) {
    if (Height < 0.1 * iniHeight) return false;
    else return true;
}
private void ZoomOutExecute(object obj) {
    Height *= 0.8;
    Width *= 0.8;
}

private const double iniWidth = 500;
private double width = iniWidth;
public double Width {
    get {
        return width;
    }
    set {
        width = value;
        NotifyPropertyChanged("Width");
    }
}

private const double iniHeight = 500;
private double height = iniHeight;
public double Height {
    get {
        return height;
    }
    set {
        height = value;
        NotifyPropertyChanged("Height");
    }
}

【问题讨论】:

  • 这个逻辑应该在控件或附加行为中。然后使用ScaleTransform。请注意,缩放和缩放是两个不同的东西。

标签: c# wpf binding


【解决方案1】:

双击可以这样绑定命令

<Button>
    <Button.InputBindings>
        <MouseBinding Gesture="LeftDoubleClick" Command="{Binding YourCommand}" />
    </Button.InputBindings>
</Button>

【讨论】:

    猜你喜欢
    • 2018-05-18
    • 2014-05-23
    • 2018-04-11
    • 2015-07-07
    • 2013-12-11
    • 1970-01-01
    • 2022-11-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多