How to bind WPF radio button selection to Viewmodel using convertes?

I have a WPF-MVVM application...

I have 3 Radio button controls - with three options => "Individual", "Group" and "Both". All 3 have same Group name...that means only one of these radio button can be selected.

I can have three properties in viewmodel...for each of these three options...and can check which one is selected.

Function()
{
  if (Is_Individual_property)
  {
   // Individual selected
  }

  if (Is_Group_property)
  {
   // group selected
  }
  if (Is_Both_property)
  {
   // Both selected
  }
}

But I think this is not best approach.

Can I have just one property in viewmodel and bind the values accordingly ?

Answers


How about having a single property and managing multiple values using a converter. For example:

XAML:

<Grid>
    <Grid.Resources>
        <local:BooleanToStringValueConverter x:Key="BooleanToStringValueConverter" />
    </Grid.Resources>
    <StackPanel>
        <TextBlock Text="{Binding Property1}" />
        <RadioButton Name="RadioButton1"
                     GroupName="Group1"
                     Content="Value1"
                     IsChecked="{Binding Path=Property1, Converter={StaticResource BooleanToStringValueConverter}, ConverterParameter=Value1}" />
        <RadioButton Name="RadioButton2"
                     GroupName="Group1"
                     Content="Value2"
                     IsChecked="{Binding Path=Property1, Converter={StaticResource BooleanToStringValueConverter}, ConverterParameter=Value2}" />
        <RadioButton Name="RadioButton3"
                     GroupName="Group1"
                     Content="Value3"
                     IsChecked="{Binding Path=Property1, Converter={StaticResource BooleanToStringValueConverter}, ConverterParameter=Value3}" />
    </StackPanel>
</Grid>

Converter:

public class BooleanToStringValueConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (System.Convert.ToString(value).Equals(System.Convert.ToString(parameter)))
        {
            return true;
        }
        return false;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (System.Convert.ToBoolean(value))
        {
            return parameter;
        }
        return null;
    }
}

Class:

public class MyClass : INotifyPropertyChanged
{
    private String _property1;
    public String Property1
    {
        get { return _property1; }
        set { _property1 = value; RaisePropertyChanged("Property1"); }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected void RaisePropertyChanged(String propertyName)
    {
        PropertyChangedEventHandler temp = PropertyChanged;
        if (temp != null)
        {
            temp(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

Window:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        DataContext = new MyClass() { Property1 = "Value1" };
    }
}

Need Your Help

How to loop text file each line and append other text wrap each line

bash shell

How to loop text file each line and append other text wrap each line in shell

jQuery click/toggle reveal / hide div panels

javascript jquery html css toggle

I've trying to click reveal and hide some div's using jQuery. I've almost got it working, just a small bit i need help with.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.