我想让WPF应用程序的用户动态地将项目添加到画布区域。为此,我在画布上添加了带有命令绑定的上下文菜单,如下所示:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
public class LoginScreen extends Application {
@Override
public void start(final Stage stage) {
WebView browser = new WebView();
WebEngine webEngine = browser.getEngine();
webEngine.load("https://jsp-password-checking-unibas.herokuapp.com");
Scene scene = new Scene(browser, 800, 600);
stage.setTitle("Login Example");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
在我的<Canvas Background="AliceBlue" Name="DensoWorkingArea">
<Canvas.ContextMenu>
<ContextMenu>
<MenuItem Header="Add New Platform" Command="{Binding AddPlatformCommand}" CommandParameter="{Binding ElementName=DensoWorkingArea}">
<MenuItem.Icon>
<Image Source="\ExternalResources\Icons\Plus.ico" />
</MenuItem.Icon>
</MenuItem>
</ContextMenu>
</Canvas.ContextMenu>
</Canvas>
中,我有一个ViewModel
:
RelayCommand
被调用的方法如下:
public ICommand AddPlatformCommand
{
get
{
return addPlatformCommand ?? (addPlatformCommand = new RelayCommand(command => AddPlatformCommandClicked(command)));
}
}
我的问题是,传递的/// <summary>
/// Method to add a new platform to the canvas
/// </summary>
public void AddPlatformCommandClicked(object parameter)
{
Canvas canvas = parameter as Canvas;
if (canvas == null) return;
Point mousePos = Mouse.GetPosition(canvas);
PlacementConfiguration.EquipmentPlacementList.Add(new EquipmentPlatformModel(new Point(0, 0)));
}
参数始终为parameter
。
我已经尝试在XAML中将相对源用作null
,因为在我将上下文菜单作为方法中的参数时,它可以工作。但是,我仍然需要知道鼠标相对于画布的位置。
我还尝试过使用Windows blend的另一条路线,但是从那儿的click事件来看,我也想不出如何在画布中获得鼠标的位置。
问题是如何在CommandParameter
方法中获得鼠标位置,以便随后将项目放置在正确的位置。