本文介绍了动态按钮的传递参数-MVVM指示灯的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

以下代码成功地动态创建了两个按钮,我不知道是如何在单击时使按钮打开不同的文件.

The following code successfully creates two buttons dynamically, what I can not figure out is how to make the buttons open a different files when clicked.

我想念什么?

<ItemsControl ItemsSource="{Binding DataButtons}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Button Content="{Binding ButtonName}"
                    Command="{Binding ButtonCommand}"
                    CommandParameter="{Binding FilePath}"/>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

ViewModel:

namespace DynamicControlsMvvmLight.ViewModel
{
    public class MainViewModel : ViewModelBase
    {
        private readonly ObservableCollection<ButtonModel> _dataButtons = new ObservableCollection<ButtonModel>();
        public ObservableCollection<ButtonModel> DataButtons { get { return _dataButtons; } }

        private ICommand _buttonCommand;
        public ICommand ButtonCommand
        {
            get {
                if (_buttonCommand == null) {
                    _buttonCommand = new RelayCommand<object>(CommandExecute, CanCommandExecute);
                }
                return _buttonCommand;
            }
        }

        public MainViewModel()
        {
            ButtonModel data1 = new ButtonModel("Button 1", ButtonCommand, "c:/Folder/File1.PDF");
            ButtonModel data2 = new ButtonModel("Button 2", ButtonCommand, "c:/Folder/File2.PDF");
            DataButtons.Add(data1);
            DataButtons.Add(data2);
        }

        private void CommandExecute(object FilePath)
        {
            ButtonModel button = FilePath as ButtonModel;
            System.Diagnostics.Process.Start(button.FilePath);
        }

        private bool CanCommandExecute(object FilePath)
        {
            Console.WriteLine("CanCommandExecute Method...");
            return true;
        }
    }
}

型号:

namespace DynamicControlsMvvmLight.Model
{
    public class ButtonModel
    {
        public string ButtonName { get; set; }
        public ICommand ButtonCommand { get; set; }
        public string FilePath { get; set; }

        public ButtonModel(string buttonName, ICommand buttonCommand, string filePath)
        {
            ButtonName = buttonName;
            ButtonCommand = buttonCommand;
            FilePath = filePath;
        }
    }
}

错误

单击任意按钮时,出现以下错误.

ERROR

I get the following error when I click any of the buttons.

推荐答案

RelayCommand希望收到CommandParameter,在这种情况下为string.

RelayCommand expects to receive CommandParameter which is a string in this case.

代码必须类似于:

        public ICommand ButtonCommand
        {
            get
            {
                if (_buttonCommand == null)
                {
                    _buttonCommand = new RelayCommand<string>(CommandExecute, CanCommandExecute);
                }
                return _buttonCommand;
            }
        }

        private void CommandExecute(string filePath)
        {
            System.Diagnostics.Process.Start(filePath);
        }

这篇关于动态按钮的传递参数-MVVM指示灯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 18:40