Full Basic

     En construccion

Farruko Cositas Que Haciamos

How to bind enum to RadioButtons

How to bind enum to RadioButtons


Windows, Windows 8, Windows Store app Development
windows8
Windows RT
en-US
6/13/2013

How to bind enum to RadioButton (CSWindowsStoreAppBindEnumToRadioButton)

Introduction

The Customer class in this sample contains a 'Sport' enum type property; the sample shows how to convert Enum type To Boolean type, and vice versa. It also shows how to bind the enum type to RadioButtons.

Building the Sample

1.       Start Visual Studio Express 2012 for Windows 8 and select File > Open > Project/Solution.

2.       Go to the directory in which you unzipped the sample. Go to the directory named for the sample, and double-click the Visual Studio Express 2012 for Windows 8 Solution (.sln) file.

3.       Press F6 or use Build > Build Solution to build the sample.

Running the Sample

1.       After launching the sample, this screen will display.

2.       Please select one of the items in GridView through right click or swipe, then click 'Edit' button in bottom Appbar, the edit page will display. Please check the Sport enum is bound to RadioButtons. You can select other RadioButton selections and click 'Save' button in bottom Appbar.

Using the Code

The code below shows how to create EnumToBoolConverter:

C#
Edit|Remove
public class EnumToBoolConverter : IValueConverter
   {
       public object Convert(object value, Type targetType, object parameter, string language)
       {
           string param = parameter as string;
           if (param == null)
               return DependencyProperty.UnsetValue;
           if (Enum.IsDefined(value.GetType(), value) == false)
               return DependencyProperty.UnsetValue;


           object paramValue = Enum.Parse(value.GetType(), param);
           return paramValue.Equals(value);
       }


       public object ConvertBack(object value, Type targetType, object parameter, string language)
       {
           string param = parameter as string;
           if (parameter == null)
               return DependencyProperty.UnsetValue;


           return Enum.Parse(typeof(Sport), param);
       }
   }

 

The code below shows the binding in xaml:

XAML
Edit|Remove
<StackPanel Grid.Row="3" Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,15,0,0">
                            <RadioButton IsChecked="{Binding Path=FavouriteSport,Converter={StaticResource ETBConverter},ConverterParameter=Basketball, Mode=TwoWay}" FontSize="25" Margin="0,0,0,10">Basketball</RadioButton>
                            <RadioButton IsChecked="{Binding Path=FavouriteSport,Converter={StaticResource ETBConverter},ConverterParameter=Football, Mode=TwoWay}" FontSize="25" Margin="0,0,0,10">Football</RadioButton>
                            <RadioButton IsChecked="{Binding Path=FavouriteSport,Converter={StaticResource ETBConverter},ConverterParameter=Baseball, Mode=TwoWay}" FontSize="25" Margin="0,0,0,10">Baseball</RadioButton>
                            <RadioButton IsChecked="{Binding Path=FavouriteSport,Converter={StaticResource ETBConverter},ConverterParameter=Swimming, Mode=TwoWay}" FontSize="25" Margin="0,0,0,10">Swimming</RadioButton>
                        </StackPanel>

 

More Information

IValueConverter Interface

http://msdn.microsoft.com/en-us/library/windows/apps/BR209903


Descargar Farruko Cositas Que Haciamos MP3 gratis en Dilandau

No hay comentarios:

Publicar un comentario en la entrada

Ingresa tus comentarios