1

First, I am new to WPF and MVVM. I have an Interface like this

Interface IItemType
{
  bool save()
}

and I have three concrete classes which they are inherited from this Interface

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

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

I have three RadioButtons (they can extend in future to 4,5 or more) in my View which I want to choose the Save Method (Class Type1 or Type2) by selecting one of these RadioButtons. The question is how can I bind these Radios to my ViewModel to not violating the pattern designs like OCP and etc (as In future I want to add more types and Radios)?

Meet the MVVM best practice design?

** EDIT **

Imagine I have the following Property

Public IItemType CurrentType { get; set; }

I want to put the Class Type1 into CurrentType property when the first Radio is selected and put the Class Type2 into CurrentType property when the second Radio is selected and so on.

Boomer
  • 1,468
  • 1
  • 15
  • 19
Mehrdad Kamelzadeh
  • 1,764
  • 2
  • 21
  • 39

2 Answers2

2

why dont you make use of RelayCommand pattern

Read about full pattern detail : WPF Apps With The Model-View-ViewModel Design Pattern public class RelayCommand : ICommand { //code realted Icommand interface }

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

Bind command like

<Hyperlink Command="{Binding Path=SaveCommand}">
Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
  • At first the user selects a Radiobutton then he/she click on a button for example to do what he/she wants. Can I use RelayCommand for that?? Shall I Put this in my concrete classes? – Mehrdad Kamelzadeh Nov 02 '12 at 06:02
  • @MehrdadKamelzadeh - its better to read the article in detail once that you get idea about this and modify the code accordingly.. – Pranay Rana Nov 02 '12 at 06:10
0

When selecting a single item from a list, a ComboBox is a nice choice. Here's some info about databinding with the ComboBox in WPF. Binding WPF ComboBox to a Custom List

Community
  • 1
  • 1
echo
  • 7,737
  • 2
  • 35
  • 33