Pass Parameter for Dynamic Buttons - MVVM Light

南楼画角 提交于 2019-12-13 08:55:54

问题


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.

What am I missing?

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

Model:

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.


回答1:


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

The code must look like:

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

and

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


来源:https://stackoverflow.com/questions/52096427/pass-parameter-for-dynamic-buttons-mvvm-light

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!