【发布时间】:2017-05-01 14:20:40
【问题描述】:
为什么我没有将参数接收到我的委托命令中?几天来我一直在关注这个问题,但仍然无法理解它,我对 .NET 框架还比较陌生。我正在使用 MVVM 和命令委托来获取单击按钮形状(如圆形)的 x 和 Y 坐标。但是,我的不断失败。如果可能的话,请帮助我。 这是我的看法
<Button Width="20"
Height="20"
Command="{Binding Path=DataContext.touchCommand,RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}">
这是我的视图模型
public class ViewModel : INotifyPropertyChanged
{
#region constructor plus member val
public event PropertyChangedEventHandler PropertyChanged;
public ObservableCollection<CartesianPoint> points { get; set; }
public ObservableCollection<CartesianPoint> missPoints { get; set; }
public DelegateCommand<CartesianPoint> SelectCommand { get; set; }
public CartesianPoint SelectedPoint { get; set; }
public ViewModel()
{
points = new ObservableCollection<CartesianPoint>();
missPoints = new ObservableCollection<CartesianPoint>();
this.testData();
}
private void InitializeCommands()
{
SelectCommand = new DelegateCommand<CartesianPoint>( (p) => SelectedPoint = p );
}
#endregion constructor plus member val
protected void OnPropertyChanged(String propertyName)
{
if( PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
Console.WriteLine( "from on propertity change");
}
public ICommand touchCommand
{
get
{
if (SelectCommand == null)
{
SelectCommand = new DelegateCommand<CartesianPoint>(( p) => Executed( p));
}
return SelectCommand;
}
}
public bool CanExecute()
{
return false;
}
public void Executed(object sender)
{
if (sender != null)
{
Console.WriteLine("this is a test");
CartesianPoint input = sender as CartesianPoint;
Console.WriteLine("{0} {1}"input.X, input.Y);
}
}
【问题讨论】: