【问题标题】:Command delegate parameter命令委托参数
【发布时间】: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);

        }
    }

【问题讨论】:

    标签: c# wpf mvvm


    【解决方案1】:

    那是因为您没有指定 CommandParameter 值。

    【讨论】:

    • 我已经添加了它,但它一直在抱怨。这就是我添加的 CommandParameter="{Binding ElementName=drilable,Path=Source}
    • @JoseGonzalez 你希望互联网上的某个人已经看到了你的 XAML 文件,并且知道 drilable 是什么。
    • @EdPlunkett 感谢您的贬低按摩,您非常乐于助人,在否决我的问题之前,您是否停止认为 XAML 很大,我可能一直试图避免产生太多噪音?再次感谢您!
    • @JoseGonzalez 如果您拒绝告诉我们您的代码是什么,您是否停止认为没有人能猜出您的代码有什么问题?
    • 提醒一下怎么样?我知道我是这里的新手,并且我正在寻求帮助,但是当我提出问题时,我并没有使该部分变暗,是的,这对我来说是多余的,但是(体面地)指出,您不明白参考应该足以让我纠正问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-28
    • 2017-12-21
    • 1970-01-01
    • 2018-11-23
    • 1970-01-01
    相关资源
    最近更新 更多