Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6950899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:09:01+00:00 2026-05-27T14:09:01+00:00

I reproduced this issue in a pretty simple .NET 4.0 test project. Using MVVM,

  • 0

I reproduced this issue in a pretty simple .NET 4.0 test project.

Using MVVM, I have a view with a user control on it. When the user clicks a button, I need to replace the user control’s data context so it points to another instance of the same class. When that happens, the value of a bound radio button in the user control is reset, i.e. the ConvertBack method of my converter BooleanToRadioButtonConverter is called with a value of false.

My questions are: why does this happen and how do I work around it?

I want to preserve the state of the ViewModels and be able to switch back and forth.

Here is my test project’s code:

The parent view:

   <StackPanel>
      <converters:RadioButtons DataContext="{Binding RadioButtonVM, Mode=OneWay}" />
      <TextBlock Margin="20 0 0 0" VerticalAlignment="Bottom">
                    <Hyperlink Command="{Binding SwitchViewModel}" >
                        <Run Text="Switch ViewModel" />
                    </Hyperlink>
      </TextBlock>
   </StackPanel>

The parent ViewModel:

   public class ParentViewModel : ViewModelBase
   {
      private ChildViewModel _vm1;
      private ChildViewModel _vm2;
      private ChildViewModel _currentChildVM;

      public ParentViewModel()
      {
         _vm1 = new ChildViewModel();
         _vm2 = new ChildViewModel();
         _currentChildVM = _vm1;
      }

      public ChildViewModel RadioButtonVM
      {
         get { return _currentChildVM; }
      }

      public ICommand SwitchViewModel
      {
         get
         {
            return new RelayCommand(p =>
            {
               if (_currentChildVM == _vm1)
                  _currentChildVM = _vm2;
               else
                  _currentChildVM = _vm1;

               // Update the view
               OnPropertyChanged("RadioButtonVM");
            });
         }
      }

The child view:

   <StackPanel Orientation="Horizontal">
      <RadioButton Name="IsProtectedGroupFalse" GroupName="IsProtectedGroup" Margin="10 0 0 0"
                                 IsChecked="{Binding IsProtected, Mode=TwoWay, Converter={StaticResource BooleanToRadioButtonConverter}, ConverterParameter=false}">
         <Label Content="False" Target="{Binding ElementName=IsProtectedGroupFalse}" />
      </RadioButton>
      <RadioButton Name="IsProtectedGroupTrue" GroupName="IsProtectedGroup" Margin="10 0 0 0"
                                 IsChecked="{Binding IsProtected, Mode=TwoWay, Converter={StaticResource BooleanToRadioButtonConverter}, ConverterParameter=true}">
         <Label Content="True" Target="{Binding ElementName=IsProtectedGroupTrue}" />
      </RadioButton>
   </StackPanel>

The child ViewModel:

   public class ChildViewModel : ViewModelBase
   {
      bool _isProtected;

      public bool IsProtected
      {
         get
         {
            return _isProtected;
         }
         set
         {
            if (_isProtected == value)
               return;

            _isProtected = value;
            OnPropertyChanged("IsProtected");
         }
      }
   }

The converter:

   class BooleanToRadioButtonConverter : IValueConverter
   {
      public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
      {
         // Debug.WriteLine("    " + DateTime.Now.ToString("HH:mm:ss:fff") + "  BooleanToRadioButtonConverter::Convert");

         bool param = bool.Parse(parameter.ToString());
         if (value == null)
            return DependencyProperty.UnsetValue;
         else
            return !((bool)value ^ param);
      }

      public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
      {
         Debug.WriteLine("    " + DateTime.Now.ToString("HH:mm:ss:fff") + "  BooleanToRadioButtonConverter::ConvertBack, value=" + value.ToString() + ", parameter=" + parameter.ToString());

         bool param = bool.Parse(parameter.ToString());
         if (value == null)
            return DependencyProperty.UnsetValue;
         else
            return !((bool)value ^ param);
      }
   }

Update

Following Rachel’s advice I replaced the RadioButtons with a CheckBox where the problem described above does not occur. I still do not know why it works the way it does, though.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T14:09:02+00:00Added an answer on May 27, 2026 at 2:09 pm

    I fixed the issue for the time being by adding an “active” property to the child ViewModel and manually setting that to true or false depending on whether the ViewModel is currently bound to the View or not. I do not consider that an ideal solution, though.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've come across this issue in my application, and have reproduced it using Apple's
I'm seeing this issue . I have a relatively simple ASP.NET page with a
Here's the simple test to reproduce the issue, this is the single class in
The project is ASP.NET 2.0, I have never been able to reproduce this myself,
Setup : Xcode 4.3.1 (or 5.x) OS X 10.7.3 I have reproduced this issue
I have issue that is reproduced on g++. VC++ doesn't meet any problems. So
ASP.NET 4.0 Need some help with this vexing HTTP POST problem - I have
Never faced this issue before. I am using a navigation controller in my app.
I have an ASP.NET repository site running on IIS6.0 and I have an issue
I'm developing an Eclipse plug-in and I have a problem which is reproduced here:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.