从UI中选择类型的最佳实践

时间:2012-11-02 05:49:14

标签: c# wpf design-patterns mvvm

首先,我是WPF和MVVM的新手。我有这样的界面

Interface IItemType
{
  bool save()
}

我有三个具体的类,它们是从这个接口继承而来的

Public Class Type1:IItemType
{
   public bool save()
   {
     //save something with method 1
   }
}

Public Class Type2:IItemType
{
   public bool save()
   {
     //save something with method 2
   }
}

我的视图中有三个RadioButtons(它们可以在将来扩展到4,5或更多),我想通过选择其中一个来选择保存方法(类Type1Type2)单选按钮。 问题是如何将这些无线电绑定到我的ViewModel,以免违反OCP等模式设计(因为将来我想添加更多类型和无线电)?

符合MVVM最佳实践设计?

**编辑**

想象一下,我有以下属性

Public IItemType CurrentType { get; set; }

我想在选择第一个Radio时将Class Type1放入CurrentType属性 并在选择第二个Radio时将Class Type2置于CurrentType属性中,依此类推。

2 个答案:

答案 0 :(得分:2)

为什么不使用RelayCommand模式

了解完整模式细节:WPF Apps With The Model-View-ViewModel Design Pattern     公共类RelayCommand:ICommand     {      //代码实现的Icommand接口     }

RelayCommand _saveCommand;
public ICommand SaveCommand
{
    get
    {
        if (_saveCommand == null)
        {
            _saveCommand = new RelayCommand(param => this.Save(),
                param => this.CanSave );
        }
        return _saveCommand;
    }
}

绑定命令,如

<Hyperlink Command="{Binding Path=SaveCommand}">

答案 1 :(得分:0)

从列表中选择单个项目时,ComboBox是一个不错的选择。以下是有关使用WPF中的ComboBox进行数据绑定的一些信息。 Binding WPF ComboBox to a Custom List

相关问题