【发布时间】: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。请注意,缩放和缩放是两个不同的东西。