How to handle binding in CheckBox and RadioButton

  • How to handle binding in CheckBox?
    There are several ways to do that binding but the best way I found is the following where the Click event is handled setting a specific ClickMode:

    <CheckBox x:Name="checkBoxMergeAudio" IsChecked="{Binding IsMergeAudio, Mode=TwoWay}" Margin="0,0,0,0" Click="checkBoxMergeAudio_Changed" ClickMode="Release">
         <TextBlock TextWrapping="Wrap" Text="{Binding LocalizedResources.MergeAudio, Source={StaticResource LocalizedStrings}}" />
    </CheckBox>
    

    and in the code behind:

    private void checkBoxMergeAudio_Changed(object sender, RoutedEventArgs e) {
      //save the changed state of the checkbox
      Utility.SetAppSettings(Constants.MergeAudio, (((SettingsPivotDataSource)DataContext).IsMergeAudio).ToString());
    }
    

    Note that using the TextBox style of the CheckBox, I succeded to handle also the possible wrapping of the text field of that control.

  • How to handle binding in RadioButton?
    Also in this case there are several ways! … one can be the following:

     <StackPanel Margin="0,0,0,0" Grid.Row="0" Background="{StaticResource BrushTransparentGray}">
                            <RadioButton Content="{Binding LocalizedResources.AlwaysLogin, Source={StaticResource LocalizedStrings}}" Name="alwaysLoginButton" IsChecked="{Binding LoginType, Converter={StaticResource EqualityConverter}, ConverterParameter=0, Mode=OneWay}" GroupName="AutoLoginGroup" Click="alwaysLoginButton_Checked" ClickMode="Release"  />
                            <RadioButton Content="{Binding LocalizedResources.DailyLogin, Source={StaticResource LocalizedStrings}}" Name="daylyLoginButton" IsChecked="{Binding LoginType, Converter={StaticResource EqualityConverter}, ConverterParameter=1, Mode=OneWay}"  GroupName="AutoLoginGroup" Click="daylyLoginButton_Checked" ClickMode="Release" />
                            <RadioButton Content="{Binding LocalizedResources.TimeStampLogin, Source={StaticResource LocalizedStrings}}" Name="loginEveryButton" IsChecked="{Binding LoginType, Converter={StaticResource EqualityConverter}, ConverterParameter=2, Mode=OneWay}" GroupName="AutoLoginGroup" Click="loginEveryButton_Checked" ClickMode="Release" />
    </StackPanel>
    

    and in the code behind:

     private void alwaysLoginButton_Checked(object sender, RoutedEventArgs e) {
                LoginTypeWrapper actualLoginTypeWrapper = new LoginTypeWrapper(LoginType.ALWAYS);
                if (!DataSourceManager.SettingsPivotDataSource.LoginType.Equals(actualLoginTypeWrapper)) {
                    DataSourceManager.SettingsPivotDataSource.LoginType = actualLoginTypeWrapper;
                    //salvo la nuova impostazione scelta dall'utente
                    Utility.SetAppSettings(Constants.LoginType, LoginType.ALWAYS.ToString());
                }
            }
    
            private void daylyLoginButton_Checked(object sender, RoutedEventArgs e) {
                LoginTypeWrapper actualLoginTypeWrapper = new LoginTypeWrapper(LoginType.DAILY);
                if (!DataSourceManager.SettingsPivotDataSource.LoginType.Equals(actualLoginTypeWrapper)) {
                    DataSourceManager.SettingsPivotDataSource.LoginType = actualLoginTypeWrapper;
                    //salvo la nuova impostazione scelta dall'utente
                    Utility.SetAppSettings(Constants.LoginType, LoginType.DAILY.ToString());
                }
            }
    
            private void loginEveryButton_Checked(object sender, RoutedEventArgs e) {
                LoginTypeWrapper actualLoginTypeWrapper = new LoginTypeWrapper(LoginType.EVERY);
                if (!DataSourceManager.SettingsPivotDataSource.LoginType.Equals(actualLoginTypeWrapper)) {
                    DataSourceManager.SettingsPivotDataSource.LoginType = actualLoginTypeWrapper;
                    //salvo la nuova impostazione scelta dall'utente
                    Utility.SetAppSettings(Constants.LoginType, LoginType.EVERY.ToString());
                }
            }
    

    where the converter is the following:

        public class EqualityConverter : IValueConverter {
    
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
               if (parameter != null && value is LoginTypeWrapper) {
                    if (((LoginTypeWrapper)value).Code == System.Convert.ToInt32(parameter)) {
                        return true;
                    }
                }
                return false;
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
                throw new NotImplementedException();
            }
        }
    
Annunci

Informazioni su Enzo Contini

Electronic engineer
Questa voce è stata pubblicata in Windows Phone/Mobile e contrassegnata con , , , . Contrassegna il permalink.

Lascia un Commento/Leave a comment

Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione / Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione / Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione / Modifica )

Google+ photo

Stai commentando usando il tuo account Google+. Chiudi sessione / Modifica )

Connessione a %s...