【问题标题】:Pass Parameter for Dynamic Buttons - MVVM Light动态按钮的传递参数 - MVVM Light
【发布时间】:2019-02-05 08:43:06
【问题描述】:

以下代码成功动态创建了两个按钮,我想不通的是如何让按钮在点击时打开不同的文件。

我错过了什么?

XAML:

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

视图模型:

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

错误

单击任何按钮时出现以下错误。

【问题讨论】:

  • 检查object FilePath。它应该是string。将其转换为字符串以获取文件路径
  • 知道了,将ButtonModel button = FilePath as ButtonModel; 更改为string button = FilePath as String;,它成功了。非常感谢。
  • 如果您不了解问题的本质。看我的回答
  • @ThierryV - 感谢您展示并指出根本原因。

标签: c# wpf mvvm data-binding mvvm-light


【解决方案1】:

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

【讨论】:

  • @ Thierry V - 非常感谢您的帮助。快速提问,此时代码工作正常,可以满足我的需要,但我想知道我的代码结构是否正常,换句话说,我是否正确使用了 ICommandRelayCommand
  • 在我看来,我认为这很好。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-11
  • 1970-01-01
  • 1970-01-01
  • 2015-01-20
  • 2018-05-18
相关资源
最近更新 更多