Sandeep Chauhan
Sandeep Chauhan

Reputation: 1313

How To change Selected Radio Button in windows Phone 8?

I am trying to implement localisation in my wp8 app. I have two radio buttons (Arabic and English) when a user selects one of the radio button then the pop up appears that is he sure he want to change language of the app if he chooses ok then the language of the app changes but if he chooses cancel then there will be no change in the language but the issue is that the radio button toggles even if user presses cancel.

How to stop the toggle to happen ?

Here is my Xaml code and the code which is binded if any radio button is checked ..

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <StackPanel Orientation="Vertical" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="20">
            <RadioButton  Content="English" Foreground="Black" FontSize="30" IsChecked="{Binding isenglishchecked,Mode=TwoWay, Source={StaticResource appSettings}}" Checked="RadioButton_Checked"/>
            <RadioButton  Content="Arabic" Foreground="Black" FontSize="30" IsChecked="{Binding isarabicchecked,Mode=TwoWay, Source={StaticResource appSettings}}" Checked="RadioButton_Checked"/>
        </StackPanel>
    </Grid>



 public bool isenglishchecked
        {
            get
            {
                return GetValueOrDefault<bool>(isenglishcheckedname, isenglishcheckedDefault);
            }
            set
            {
                var result = MessageBox.Show("This Will Change the Language of the App Do you Want to continue ?", "Language Change", MessageBoxButton.OKCancel);

                if (result == MessageBoxResult.OK)
                {
                    if (AddOrUpdateValue(isenglishcheckedname, value))
                    {
                        Save();
                        if (isenglishchecked)
                        {
                            Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
                            Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en-US");

                            MainViewModel.stationnamelist = null;
                            Messenger.Default.Send<string>("mainpage", "tomainpage");
                            (Application.Current.RootVisual as PhoneApplicationFrame).Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
                        }

                        else
                        {
                            Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("ar-AE");
                            Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("ar-AE");

                            MainViewModel.stationnamelist = null;
                            Messenger.Default.Send<string>("mainpage", "tomainpage");
                            (Application.Current.RootVisual as PhoneApplicationFrame).Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
                        }
                    }
                }
                else
                {
                    (Application.Current.RootVisual as PhoneApplicationFrame).Navigate(new Uri("/Skins/SelectLanguage.xaml", UriKind.Relative));
                }
            }
        }

        public bool isarabicchecked
        {
            get
            {
                return GetValueOrDefault<bool>(isarabiccheckedname, isarabiccheckedDefault);
            }
            set
            {
                if (AddOrUpdateValue(isarabiccheckedname, value))
                {
                    Save();
                }
            }
        }

Upvotes: 0

Views: 3442

Answers (1)

Mike Fischer
Mike Fischer

Reputation: 206

What's likely happening is that the UI layer (Silverlight), in response to the user's tap, happily changes its own state, and then sets the value of isenglishchecked or isarabicchecked. The fact that the value of the property doesn't change is of no consequence -- Silverlight doesn't notice. (not changing the value of the property in response to a set call is non-standard behavior, so it's not surprising that Silverlight would "misbehave" like this)

There are a few things you can try:

  1. In the set handler for both properties, raise the PropertyChanged event (in INotifyPropertyChanged) for isenglishchecked and isarabicchecked. This will cause the UI to re-query the view model so it's back in sync with it. If the checked properties didn't change, the UI will notice this.
  2. Use the built-in ExceptionValidationRule on each RadioButton, then throw an exception in the Set handler if the user cancels the operation. This might cause the radio button's value not to change (good), but it might also make your button red.
  3. Define your own ValidationRule. It might work better to show the message box during ValidationRule.Validate.
  4. Ditch the confirmation dialog entirely and just change the language immediately. If it was a mistake, the user can just tap the other button.

Upvotes: 1

Related Questions