ccdreyer
ccdreyer

Reputation: 66

C# MVVM: Binding a RadioButton to a boolean Property

I am quiet new to programming and am currently learning C# and the MVVM pattern.

I need to code a database tool for ChiliPlants for university. There you should be able to add a new object to an ObservableCollection.

To add a new Item to this ObservableCollection a new Window opens. It looks like this: Window Add

I now want the two RadioBoxes to be bound to a property called "HybridSeed". Which is defined in the ViewModel:

//Public Property HybridSeed
    public bool HybridSeed
    {
        get { return ChiliModel.HybridSeed; }
        set
        {
            if (ChiliModel.HybridSeed == value)
                return;
            ChiliModel.HybridSeed = value;
            OnPropertyChanged("HybridSeed");
        }
    }

The RadioBox part of my View looks like this:

 <RadioButton Grid.Row="5" Content="Ja" Grid.Column="1" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top"/>
    <RadioButton Grid.Row="5" Content="Nein" Grid.Column="1" HorizontalAlignment="Left" Margin="89,10,0,0" VerticalAlignment="Top"/>

But how to bind the outcome of a user clicking on these RadioButtons to this HybridSeed Property? Important is that the outcome is a bool.

I looked up almost every entry similar to this topic, but I did not find a simple solution. Or a solution which I was able to understand with my bad coding skills :( ...

I would be very happy if you guys could help me. Please keep it simple for this newbie :)

If there is a simpler solution using a CheckBox or a ComboBox it would also be perfect. The most important thing is to have a nice user interface. Right now it only works with a TextBox where the user always has to write "True" or "False".

Solution:

I added the IsClicked Property in the "Yes" RadioButton to be bound to my boulean property with: IsClicked="{Binding HybridSeed}". Thanks to naslund for his fast answer :)

Upvotes: 3

Views: 4676

Answers (1)

naslund
naslund

Reputation: 625

Just bind HybridSeed to the Yes-radiobutton. It will then either be true if the user has selected that or false if No-radiobutton has been selected (or if nothing has been selected). Binding to both buttons in this case is a bit redundant since the mechanism of radiobuttons takes care of it.

WPF:

<RadioButton Content="Yes" IsChecked="{Binding HybridSeed}" />
<RadioButton Content="No" />
<Label Content="{Binding HybridSeed}" ContentStringFormat="Value is: {0}" />

Logic:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = new ViewModel();
    }
}

public class ViewModel : INotifyPropertyChanged
{
    private bool hybridSeed;

    public bool HybridSeed
    {
        get { return hybridSeed; }
        set
        {
            hybridSeed = value;
            OnPropertyChanged(nameof(HybridSeed));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

Upvotes: 7

Related Questions