C# 使用ICommand更改ViewModel的状态
我想使用ICommand更改ViewModel的palle1.Y int值。我应该创建一个实现ICommand接口的类吗?我已经做到了。但由于它是一个类,因此如果不为其创建属性,它就无法访问我的ViewModel的Papper1属性。因此,我更愿意在ViewModel中创建命令。此时,我正试图将palle1作为XAML中的CommandParameter传递给命令。我在这方面失败了,我也不确定这是编辑ViewModel状态的最干净的方法 我可以得到一个UpKeyPressed命令绑定到按钮或键盘up键的代码示例吗?如果该命令可以访问我的ViewModel Papper1属性,那么如果没有CommandParameter,则会更干净 我的ViewModel:C# 使用ICommand更改ViewModel的状态,c#,wpf,mvvm,data-binding,icommand,C#,Wpf,Mvvm,Data Binding,Icommand,我想使用ICommand更改ViewModel的palle1.Y int值。我应该创建一个实现ICommand接口的类吗?我已经做到了。但由于它是一个类,因此如果不为其创建属性,它就无法访问我的ViewModel的Papper1属性。因此,我更愿意在ViewModel中创建命令。此时,我正试图将palle1作为XAML中的CommandParameter传递给命令。我在这方面失败了,我也不确定这是编辑ViewModel状态的最干净的方法 我可以得到一个UpKeyPressed命令绑定到按钮或键盘
namespace Pong.Core.ViewModels
{
public class GamePlayViewModel
{
private readonly Paddle Paddle1;
private Paddle Paddle2;
public GamePlayViewModel()
{
Paddle1 = new Paddle();
Paddle2 = new Paddle();
UpKeyPressed();
}
public ICommand UpKeyPressed()
{
var r = new UpKeyPressed();
r.Execute(Paddle1);
return r;
}
}
public class UpKeyPressed : ICommand
{
public void Execute(object parameter)
{
var paddle = parameter as Paddle;
Debug.Assert(paddle != null, "paddle != null");
paddle.IncreaseY();
Debug.WriteLine(paddle.Y);
}
public bool CanExecute(object parameter)
{
return parameter != null;
}
public event EventHandler CanExecuteChanged;
}
}
使用viewmodel作为dataContext的我的XAML页面:
<Window x:Class="Pong.Windows.Views.GamePlayView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Pong.Core.ViewModels;assembly=Pong.Core"
Title="GamePlayView" Height="350" Width="525">
<Grid>
<Button CommandParameter="{Binding ElementName=Paddle1}"
Command="{StaticResource UpKeyPressed}" >
Click
</Button>
</Grid>
<Window.DataContext>
<local:GamePlayViewModel/>
</Window.DataContext>
<Window.InputBindings>
<KeyBinding Command="{Binding Path=UpKeyPressed}"
Key="O"
Modifiers="Control"/>
</Window.InputBindings>
</Window>
按下“O”键时,XAML尝试无错误但不工作:
<Window x:Class="Pong.Windows.Views.GamePlayView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:viewModels="clr-namespace:Pong.Core.ViewModels;assembly=Pong.Core"
Title="GamePlayView" Height="350" Width="525">
<Grid>
</Grid>
<Window.DataContext>
<viewModels:GamePlayViewModel/>
</Window.DataContext>
<Window.InputBindings>
<KeyBinding Command="{Binding DoSomethingCommand}"
Key="O"
Modifiers="Control"/>
</Window.InputBindings>
查看您的尝试,我们需要解决一些问题,首先您的CanExecute不应再涉及参数: 其次,您的XAML绑定是错误的,您的视图模型的DataContext已经在可视化树中运行,您只需要一个简单的绑定,并指定如下路径:
<KeyBinding Command="{Binding DoSomethingCommand}"
Key="O"
Modifiers="Control"/>
在视图模型中只按了1个方法UpKeyPressed,它不能用于绑定。您需要声明一个属性。此外,UpKeyPressed命令应该有一些属性接受您的papper1,您需要在初始化视图模型类中的命令时设置该属性。@KingKing您的意思是我在ViewModel中创建一个UpKeyPressed类型的属性,然后在UpKeyPressed类中创建一个名为Papper1的属性,并为该类提供一个设置Papper1属性的构造函数?类似于此,但您应该设置在初始化命令之后的pable1属性通常在upkeypress的getter中property@KingKing我试图将其修正并粘贴到问题中。还没开始工作。干杯,谢谢。我在UpKeyPressed类的execute方法中有一个断点,当按下o键时,它仍然不会触发。显示时没有错误。@Ben我刚刚测试了它并确认它应该可以工作。您是否将DataContext设置为与代码隐藏不同的内容?这是我的代码隐藏:公共部分类GamePlayView:Window{public GamePlayView{InitializeComponent;DataContext=new GamePlayViewModel;}您是否可以在输出窗口中查看,以检查是否有任何异常以静默方式抛出?我无法找到或打开PDB文件。大约5个dll文件。
public bool CanExecute(object parameter) {
return Paddle1 != null;
}
<KeyBinding Command="{Binding DoSomethingCommand}"
Key="O"
Modifiers="Control"/>