Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVVM radiobuttons

Someone please help. I have an interesting issue. I am trying to implement an MVVM app and I want to bind to radiobuttons in my view.

Here's my view:

<StackPanel Orientation="Horizontal" Grid.ColumnSpan="2"  >     <RadioButton GroupName="1" IsChecked="{Binding Path=NoteGeneral, Mode=TwoWay}">General</RadioButton>     <RadioButton GroupName="1" IsChecked="{Binding Path=NoteContact, Mode=TwoWay}" >Contact</RadioButton>     <RadioButton GroupName="1" IsChecked="{Binding Path=NoteAddress, Mode=TwoWay}" >Address</RadioButton>     <RadioButton GroupName="1" IsChecked="{Binding Path=NotePhone, Mode=TwoWay}" >Phone</RadioButton> </StackPanel> 

Here's my ViewModel:

    bool _NoteGeneral;     public bool NoteGeneral     {         get { return _NoteGeneral; }         set         {             _NoteGeneral = value;             OnPropertyChanged("NoteGeneral");         }     }      bool _NoteContact;     public bool NoteContact     {         get { return _NoteContact; }         set         {             _NoteContact = value;             OnPropertyChanged("NoteContact");         }     }      bool _NoteAddress;     public bool NoteAddress     {         get { return _NoteAddress; }         set         {             _NoteAddress = value;             OnPropertyChanged("NoteAddress");         }     }      bool _NotePhone;     public bool NotePhone     {         get { return _NotePhone; }         set         {             _NotePhone = value;             OnPropertyChanged("NotePhone");         }     } 

The problem is this, when I click the different radiobuttons the property setter only gets called the first time(when i run thru debugging). e.g. When I click NoteGeneral, NoteContact, then NoteGeneral again only the first two clicks update my viewmodel. I think I might have something wrong with my binding, or maybe I'm approaching this the completely wrong way.

Can anyone help?

How should I implement radiobutton selections in my viewmodel?

.NET 4 and Later

This issue with RadioButton binding was resolved by Microsoft when .NET 4 was released. Binding of RadioButtons now works as you would expect without any of the work-arounds listed below.

like image 718
Jose Avatar asked May 19 '09 14:05

Jose


People also ask

What is Mvvm command?

Commands are an implementation of the ICommand interface that is part of the . NET Framework. This interface is used a lot in MVVM applications, but it is useful not only in XAML-based apps.

How do you check whether a RadioButton is checked or not in C#?

FirstOrDefault(r => r. Checked); Note that this requires that all of the radio buttons be directly in the same container (eg, Panel or Form), and that there is only one group in the container. If that is not the case, you could make List<RadioButton> s in your constructor for each group, then write list.

How do I use radio buttons in XAML?

The <RadioButton> element in XAML is used to create a radio button control in a WPF app. The <RadioButton> element in XAML is used to create a radio button control in a WPF app. The following code example creates a radio button and sets the name, height and width of a RadioButton control.


2 Answers

Take a look here.

I haven't implemented the solution provided but it makes sense. The underlying framework control breaks you bindings when a click is performed. The solution is to override the method that does this and just rely on the bindings.

like image 194
Jab Avatar answered Sep 25 '22 10:09

Jab


Jaime Rodriguez, who works at Microsoft on WPF, publishes an unabridged Q&A on WPF, and the latest issue has a post on RadioButtons and MVVM !

The post is at http://blogs.msdn.com/jaimer/archive/2009/09/22/wpf-discussion-090922.aspx, and you want to look at the last item in that post. I tested the solution and it works to my satisfaction.

Quoted for convenience:

I've worked around this issue in .NET 3.5 SP1. Here's how I data bind a group of radio buttons to an enum-valued property:

<StackPanel>     <RadioButton Content="New folder"         IsChecked="{Binding Path=PublishTarget,                     Converter={StaticResource equalityConverter},                     ConverterParameter={x:Static local:PublishTarget.NewServerFolder}, Mode=TwoWay}"         GroupName="1" />      <RadioButton Content="Existing folder"         IsChecked="{Binding Path=PublishTarget,                     Converter={StaticResource equalityConverter},                     ConverterParameter={x:Static local:PublishTarget.ExistingServerFolder},                     Mode=TwoWay}"         GroupName="2" />      <RadioButton Content="Local folder"         IsChecked="{Binding Path=PublishTarget,                     Converter={StaticResource equalityConverter},                     ConverterParameter={x:Static local:PublishTarget.LocalFolder},                     Mode=TwoWay}"         GroupName="3" /> </StackPanel> 

Setting each radio button's GroupName to a unique value prevents the bindings from getting clobbered when the user clicks on a radio button. Here I'm relying on the data source to implement INotifyPropertyChanged, which will tell the other radio buttons to update. A similar approach should work for radio buttons in an ItemsControl.

like image 32
Thomas Dufour Avatar answered Sep 23 '22 10:09

Thomas Dufour