Параметр передачи для динамических кнопок - MVVM Light - PullRequest
0 голосов
/ 30 августа 2018

Следующий код успешно создает две кнопки динамически, но я не могу понять, как заставить кнопки открывать различные файлы при нажатии.

Что мне не хватает?

XAML:

<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

Я получаю следующую ошибку при нажатии любой из кнопок. enter image description here

1 Ответ

0 голосов
/ 30 августа 2018

RelayCommand ожидает получения CommandParameter, что в данном случае составляет string.

Код должен выглядеть так:

        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);
        }
...