Google Customizing Default RadioButton and CheckBoxes with our images in WindowsPhone C# | SubramanyamRaju Xamarin & Windows App Dev Tutorials

Friday 29 November 2013

Customizing Default RadioButton and CheckBoxes with our images in WindowsPhone C#

Generally it is not possible to set default radiobuttons and checkbox border , pressed state colors in windows phone using xaml,So we need to override the template of both radiobutton & checkboxes grid template.And so we may need to make customizable controls in some requirement with respect of our images.And it is best practice for if client want to know control pressed state with proper image background selection. And for our requirement, expression blend is best suitable tool for customizing radio button s as well as checkboxes and off course it is very flexible for generating customizable xaml of our app UI.
Note:
1)This sample is targeted on  windows phone 7.1
2)Here I am using expression blend 2012 which is comes under wp8 sdk.


Building the Sample

No need to add any other external dll's & libraries.Beacuse BLEND will comes on wp sdk by default.We need to add following images in project.
    

Source File at :  Radio&checkBoxStyles in windowsphone

Description

However expression blend is more efficient for customizing UI controls,To know more about custom style's you may visit http://www.geekchamp.com/tips/custom-styles-and-templates-in-windows-phone-intro.We need to follow some steps here
1)Deafault RadioButton and CheckBox Template is

XAML

 <Grid x:Name="DefaultPanel" Grid.Row="1" Margin="12,0,12,0"            <Grid.RowDefinitions                <RowDefinition Height="Auto"/> 
                <RowDefinition Height="Auto"/> 
                <RowDefinition Height="*"/> 
            </Grid.RowDefinitions> 
 
            <TextBlock Grid.Row="0" Text="1.Default RadioButton and Checkbox" /> 
            <RadioButton Content="RadioButton" Grid.Row="1" Height="75" Margin="0,0,266,0"/> 
            <CheckBox Content="CheckBox" Grid.Row="1" Height="75" Margin="236,0,43,0"/> 
        </Grid>
 2)Embeded RadioButton and CheckBox custom style's in <phone:PhoneApplicationPage.Resources> wich is generated from blend.
XAML

<phone:PhoneApplicationPage.Resources        <Style x:Key="PhoneButtonBase" TargetType="ButtonBase"> 
            <Setter Property="BackgroundValue="Transparent"/> 
            <Setter Property="BorderBrushValue="{StaticResource PhoneForegroundBrush}"/> 
            <Setter Property="ForegroundValue="{StaticResource PhoneForegroundBrush}"/> 
            <Setter Property="BorderThicknessValue="{StaticResource PhoneBorderThickness}"/> 
            <Setter Property="FontFamilyValue="{StaticResource PhoneFontFamilySemiBold}"/> 
            <Setter Property="FontSizeValue="{StaticResource PhoneFontSizeMediumLarge}"/> 
            <Setter Property="PaddingValue="10,3,10,5"/> 
            <Setter Property="Template"> 
                <Setter.Value> 
                    <ControlTemplate TargetType="ButtonBase"> 
                        <Grid Background="Transparent"> 
                            <VisualStateManager.VisualStateGroups> 
                                <VisualStateGroup x:Name="CommonStates"> 
                                    <VisualState x:Name="Normal"/> 
                                    <VisualState x:Name="MouseOver"/> 
                                    <VisualState x:Name="Pressed"> 
                                        <Storyboard> 
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="ForegroundStoryboard.TargetName="ContentContainer"> 
                                                <DiscreteObjectKeyFrame KeyTime="0Value="{StaticResource PhoneBackgroundBrush}"/> 
                                            </ObjectAnimationUsingKeyFrames> 
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BackgroundStoryboard.TargetName="ButtonBackground"> 
                                                <DiscreteObjectKeyFrame KeyTime="0Value="{StaticResource PhoneForegroundBrush}"/> 
                                            </ObjectAnimationUsingKeyFrames> 
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrushStoryboard.TargetName="ButtonBackground"> 
                                                <DiscreteObjectKeyFrame KeyTime="0Value="{StaticResource PhoneForegroundBrush}"/> 
                                            </ObjectAnimationUsingKeyFrames> 
                                        </Storyboard> 
                                    </VisualState> 
                                    <VisualState x:Name="Disabled"> 
                                        <Storyboard> 
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="ForegroundStoryboard.TargetName="ContentContainer"> 
                                                <DiscreteObjectKeyFrame KeyTime="0Value="{StaticResource PhoneDisabledBrush}"/> 
                                            </ObjectAnimationUsingKeyFrames> 
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrushStoryboard.TargetName="ButtonBackground"> 
                                                <DiscreteObjectKeyFrame KeyTime="0Value="{StaticResource PhoneDisabledBrush}"/> 
                                            </ObjectAnimationUsingKeyFrames> 
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BackgroundStoryboard.TargetName="ButtonBackground"> 
                                                <DiscreteObjectKeyFrame KeyTime="0Value="Transparent"/> 
                                            </ObjectAnimationUsingKeyFrames> 
                                        </Storyboard> 
                                    </VisualState> 
                                </VisualStateGroup> 
                            </VisualStateManager.VisualStateGroups> 
                            <Border x:Name="ButtonBackgroundBorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" CornerRadius="0Margin="{StaticResource PhoneTouchTargetOverhang}"> 
                                <ContentControl x:Name="ContentContainerContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" Padding="{TemplateBinding Padding}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/> 
                            </Border> 
                        </Grid> 
                    </ControlTemplate> 
                </Setter.Value> 
            </Setter> 
        </Style> 
        <Style x:Key="PhoneRadioButtonCheckBoxBase" BasedOn="{StaticResource PhoneButtonBase}" TargetType="ToggleButton"> 
            <Setter Property="BackgroundValue="{StaticResource PhoneRadioCheckBoxBrush}"/> 
            <Setter Property="BorderBrushValue="{StaticResource PhoneRadioCheckBoxBrush}"/> 
            <Setter Property="FontSizeValue="{StaticResource PhoneFontSizeMedium}"/> 
            <Setter Property="FontFamilyValue="{StaticResource PhoneFontFamilyNormal}"/> 
            <Setter Property="HorizontalContentAlignmentValue="Left"/> 
            <Setter Property="VerticalContentAlignmentValue="Center"/> 
            <Setter Property="PaddingValue="0"/> 
        </Style> 
        <Style x:Key="RadioButtonStyle1" BasedOn="{StaticResource PhoneRadioButtonCheckBoxBase}" TargetType="RadioButton"> 
            <Setter Property="Template"> 
                <Setter.Value> 
                    <ControlTemplate TargetType="RadioButton"> 
                        <Grid Background="Transparent"> 
                            <VisualStateManager.VisualStateGroups> 
                                <VisualStateGroup x:Name="CommonStates"> 
                                    <VisualState x:Name="Normal"> 
                                        <Storyboard> 
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Fill)" Storyboard.TargetName="CheckMark"> 
                                                <DiscreteObjectKeyFrame KeyTime="0"> 
                                                    <DiscreteObjectKeyFrame.Value> 
                                                        <LinearGradientBrush EndPoint="0.5,1StartPoint="0.5,0"> 
                                                            <GradientStop Color="BlackOffset="0"/> 
                                                            <GradientStop Color="#FF0C9FE4Offset="1"/> 
                                                        </LinearGradientBrush> 
                                                    </DiscreteObjectKeyFrame.Value> 
                                                </DiscreteObjectKeyFrame> 
                                            </ObjectAnimationUsingKeyFrames> 
                                        </Storyboard> 
                                    </VisualState> 
                                    <VisualState x:Name="MouseOver"/> 
                                    <VisualState x:Name="Pressed"> 
                                        <Storyboard> 
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="VisibilityStoryboard.TargetName="EnabledCheckBackground"> 
                                                <DiscreteObjectKeyFrame KeyTime="0"> 
                                                    <DiscreteObjectKeyFrame.Value> 
                                                        <Visibility>Collapsed</Visibility> 
                                                    </DiscreteObjectKeyFrame.Value> 
                                                </DiscreteObjectKeyFrame> 
                                            </ObjectAnimationUsingKeyFrames> 
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="VisibilityStoryboard.TargetName="PressedDarkCheckBackground"> 
                                                <DiscreteObjectKeyFrame KeyTime="0"> 
                                                    <DiscreteObjectKeyFrame.Value> 
                                                        <Visibility>Visible</Visibility> 
                                                    </DiscreteObjectKeyFrame.Value> 
                                                </DiscreteObjectKeyFrame> 
                                            </ObjectAnimationUsingKeyFrames> 
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="VisibilityStoryboard.TargetName="PressedLightCheckBackground"> 
                                                <DiscreteObjectKeyFrame KeyTime="0"> 
                                                    <DiscreteObjectKeyFrame.Value> 
                                                        <Visibility>Visible</Visibility> 
                                                    </DiscreteObjectKeyFrame.Value> 
                                                </DiscreteObjectKeyFrame> 
                                            </ObjectAnimationUsingKeyFrames> 
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="FillStoryboard.TargetName="CheckMark"> 
                                                <DiscreteObjectKeyFrame KeyTime="0"> 
                                                    <DiscreteObjectKeyFrame.Value> 
                                                        <LinearGradientBrush EndPoint="0.5,1StartPoint="0.5,0"> 
                                                            <GradientStop Color="BlackOffset="0"/> 
                                                            <GradientStop Color="#FFA0CCEAOffset="1"/> 
                                                        </LinearGradientBrush> 
                                                    </DiscreteObjectKeyFrame.Value> 
                                                </DiscreteObjectKeyFrame> 
                                            </ObjectAnimationUsingKeyFrames> 
                                        </Storyboard> 
                                    </VisualState> 
                                    <VisualState x:Name="Disabled"> 
                                        <Storyboard> 
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="VisibilityStoryboard.TargetName="EnabledCheckBackground"> 
                                                <DiscreteObjectKeyFrame KeyTime="0"> 
                                                    <DiscreteObjectKeyFrame.Value> 
                                                        <Visibility>Collapsed</Visibility> 
                                                    </DiscreteObjectKeyFrame.Value> 
                                                </DiscreteObjectKeyFrame> 
                                            </ObjectAnimationUsingKeyFrames> 
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="VisibilityStoryboard.TargetName="DisabledDarkCheckBackground"> 
                                                <DiscreteObjectKeyFrame KeyTime="0"> 
                                                    <DiscreteObjectKeyFrame.Value> 
                                                        <Visibility>Visible</Visibility> 
                                                    </DiscreteObjectKeyFrame.Value> 
                                                </DiscreteObjectKeyFrame> 
                                            </ObjectAnimationUsingKeyFrames> 
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="VisibilityStoryboard.TargetName="DisabledLightCheckBackground"> 
                                                <DiscreteObjectKeyFrame KeyTime="0"> 
                                                    <DiscreteObjectKeyFrame.Value> 
                                                        <Visibility>Visible</Visibility> 
                                                    </DiscreteObjectKeyFrame.Value> 
                                                </DiscreteObjectKeyFrame> 
                                            </ObjectAnimationUsingKeyFrames> 
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="FillStoryboard.TargetName="CheckMark"> 
                                                <DiscreteObjectKeyFrame KeyTime="0Value="{StaticResource PhoneRadioCheckBoxCheckDisabledBrush}"/> 
                                            </ObjectAnimationUsingKeyFrames> 
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="ForegroundStoryboard.TargetName="ContentContainer"> 
                                                <DiscreteObjectKeyFrame KeyTime="0Value="{StaticResource PhoneDisabledBrush}"/> 
                                            </ObjectAnimationUsingKeyFrames> 
                                        </Storyboard> 
                                    </VisualState> 
                                </VisualStateGroup> 
                                <VisualStateGroup x:Name="CheckStates"> 
                                    <VisualState x:Name="Checked"> 
                                        <Storyboard> 
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="VisibilityStoryboard.TargetName="CheckMark"> 
                                                <DiscreteObjectKeyFrame KeyTime="0"> 
                                                    <DiscreteObjectKeyFrame.Value> 
                                                        <Visibility>Visible</Visibility> 
                                                    </DiscreteObjectKeyFrame.Value> 
                                                </DiscreteObjectKeyFrame> 
                                            </ObjectAnimationUsingKeyFrames> 
                                        </Storyboard> 
                                    </VisualState> 
                                    <VisualState x:Name="Unchecked"/> 
                                    <VisualState x:Name="Indeterminate"/> 
                                </VisualStateGroup> 
                                <VisualStateGroup x:Name="FocusStates"/> 
                                <VisualStateGroup x:Name="ValidationStates"/> 
                            </VisualStateManager.VisualStateGroups> 
                            <Grid Margin="{StaticResource PhoneTouchTargetLargeOverhang}"> 
                                <Grid.ColumnDefinitions> 
                                    <ColumnDefinition Width="32"/> 
                                    <ColumnDefinition Width="*"/> 
                                </Grid.ColumnDefinitions> 
                                <Ellipse x:Name="EnabledCheckBackground"  HorizontalAlignment="LeftHeight="32IsHitTestVisible="FalseVerticalAlignment="CenterWidth="32"> 
                                    <Ellipse.Fill> 
                                        <ImageBrush  Stretch="UniformImageSource="/Images/radio_n_hdpi.png"/> 
                                    </Ellipse.Fill> 
                                </Ellipse> 
                                <Canvas  HorizontalAlignment="LeftHeight="32IsHitTestVisible="FalseOpacity="{StaticResource PhoneDarkThemeOpacity}" VerticalAlignment="CenterWidth="32"> 
                                    <Ellipse x:Name="PressedDarkCheckBackgroundFill="{StaticResource PhoneRadioCheckBoxPressedBrush}" Height="32Visibility="CollapsedWidth="32"/> 
                                    <Ellipse x:Name="DisabledDarkCheckBackgroundFill="{StaticResource PhoneRadioCheckBoxDisabledBrush}" Height="32Visibility="CollapsedWidth="32"/> 
                                </Canvas> 
                                <Canvas HorizontalAlignment="LeftHeight="32IsHitTestVisible="FalseOpacity="{StaticResource PhoneLightThemeOpacity}" VerticalAlignment="CenterWidth="32"> 
                                    <Ellipse x:Name="PressedLightCheckBackgroundFill="{StaticResource PhoneRadioCheckBoxPressedBrush}" Height="32Stroke="{StaticResource PhoneForegroundBrush}" StrokeThickness="{StaticResource PhoneStrokeThickness}" Visibility="CollapsedWidth="32"/> 
                                    <Ellipse x:Name="DisabledLightCheckBackgroundFill="{StaticResource PhoneRadioCheckBoxDisabledBrush}" Height="32Stroke="{StaticResource PhoneDisabledBrush}" StrokeThickness="{StaticResource PhoneStrokeThickness}" Visibility="CollapsedWidth="32"/> 
                                </Canvas> 
                                <Ellipse  x:Name="CheckMark"  HorizontalAlignment="CenterHeight="16IsHitTestVisible="FalseVisibility="CollapsedVerticalAlignment="CenterWidth="16"/> 
                                <ContentControl x:Name="ContentContainerContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Grid.Column="1Foreground="{TemplateBinding Foreground}" FontSize="{TemplateBinding FontSize}" FontFamily="{TemplateBinding FontFamily}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="12,0,0,0Padding="{TemplateBinding Padding}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/> 
                            </Grid> 
                        </Grid> 
                    </ControlTemplate> 
                </Setter.Value> 
            </Setter> 
        </Style> 
 
 
        <Style x:Key="CheckBoxStyle1" BasedOn="{StaticResource PhoneRadioButtonCheckBoxBase}" TargetType="CheckBox"> 
            <Setter Property="Template"> 
                <Setter.Value> 
                    <ControlTemplate TargetType="CheckBox"> 
                        <Grid Background="Transparent"> 
                            <VisualStateManager.VisualStateGroups> 
                                <VisualStateGroup x:Name="CommonStates"> 
                                    <VisualState x:Name="Normal"/> 
                                    <VisualState x:Name="MouseOver"/> 
                                    <VisualState x:Name="Pressed"> 
                                        <Storyboard> 
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrushStoryboard.TargetName="CheckBackground"> 
                                                <DiscreteObjectKeyFrame KeyTime="0Value="{StaticResource PhoneRadioCheckBoxPressedBorderBrush}"/> 
                                            </ObjectAnimationUsingKeyFrames> 
 
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="FillStoryboard.TargetName="IndeterminateMark"> 
                                                <DiscreteObjectKeyFrame KeyTime="0Value="{StaticResource PhoneRadioCheckBoxCheckBrush}"/> 
                                            </ObjectAnimationUsingKeyFrames> 
                                        </Storyboard> 
                                    </VisualState> 
                                    <VisualState x:Name="Disabled"> 
                                        <Storyboard> 
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrushStoryboard.TargetName="CheckBackground"> 
                                                <DiscreteObjectKeyFrame KeyTime="0Value="{StaticResource PhoneDisabledBrush}"/> 
                                            </ObjectAnimationUsingKeyFrames> 
 
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="FillStoryboard.TargetName="IndeterminateMark"> 
                                                <DiscreteObjectKeyFrame KeyTime="0Value="{StaticResource PhoneRadioCheckBoxCheckDisabledBrush}"/> 
                                            </ObjectAnimationUsingKeyFrames> 
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="ForegroundStoryboard.TargetName="ContentContainer"> 
                                                <DiscreteObjectKeyFrame KeyTime="0Value="{StaticResource PhoneDisabledBrush}"/> 
                                            </ObjectAnimationUsingKeyFrames> 
                                        </Storyboard> 
                                    </VisualState> 
                                </VisualStateGroup> 
                                <VisualStateGroup x:Name="CheckStates"> 
                                    <VisualState x:Name="Checked"> 
                                        <Storyboard> 
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="VisibilityStoryboard.TargetName="CheckMark"> 
                                                <DiscreteObjectKeyFrame KeyTime="0"> 
                                                    <DiscreteObjectKeyFrame.Value> 
                                                        <Visibility>Visible</Visibility> 
                                                    </DiscreteObjectKeyFrame.Value> 
                                                </DiscreteObjectKeyFrame> 
                                            </ObjectAnimationUsingKeyFrames> 
                                        </Storyboard> 
                                    </VisualState> 
                                    <VisualState x:Name="Unchecked"/> 
                                    <VisualState x:Name="Indeterminate"> 
                                        <Storyboard> 
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="VisibilityStoryboard.TargetName="IndeterminateMark"> 
                                                <DiscreteObjectKeyFrame KeyTime="0"> 
                                                    <DiscreteObjectKeyFrame.Value> 
                                                        <Visibility>Visible</Visibility> 
                                                    </DiscreteObjectKeyFrame.Value> 
                                                </DiscreteObjectKeyFrame> 
                                            </ObjectAnimationUsingKeyFrames> 
                                        </Storyboard> 
                                    </VisualState> 
                                </VisualStateGroup> 
                            </VisualStateManager.VisualStateGroups> 
                            <Grid  Margin="{StaticResource PhoneTouchTargetLargeOverhang}"> 
                                <Grid.ColumnDefinitions> 
                                    <ColumnDefinition Width="32"/> 
                                    <ColumnDefinition Width="*"/> 
                                </Grid.ColumnDefinitions> 
                                <Border x:Name="CheckBackground"  
                                     BorderBrush="Gray" 
                                     BorderThickness="3" 
                                      Background="WhiteHorizontalAlignment="Left" 
                                       Height="32IsHitTestVisible="FalseVerticalAlignment="Center" 
                                     Width="32"></Border> 
                                <Rectangle x:Name="IndeterminateMarkFill="{StaticResource PhoneRadioCheckBoxCheckBrush}" HorizontalAlignment="CenterHeight="16IsHitTestVisible="FalseGrid.Row="0Visibility="CollapsedVerticalAlignment="CenterWidth="16"/> 
                                <Image Source="/Images/check_s_hdpi.pngx:Name="CheckMarkHorizontalAlignment="CenterHeight="32"  
                        IsHitTestVisible="FalseStretch="Fill"  Visibility="CollapsedVerticalAlignment="CenterWidth="32"/> 
                                <ContentControl x:Name="ContentContainerContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Grid.Column="1Foreground="{TemplateBinding Foreground}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="12,0,0,0Padding="{TemplateBinding Padding}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/> 
                            </Grid> 
                        </Grid> 
                    </ControlTemplate> 
                </Setter.Value> 
            </Setter> 
        </Style> 
    </phone:PhoneApplicationPage.Resources>
3)Applying styles for both radiobutton and checkbox
XAML

<RadioButton Style="{StaticResource RadioButtonStyle1}" Content="RadioButton" Grid.Row="1" Height="75" Margin="0,0,266,0"/> 
<CheckBox Style="{StaticResource CheckBoxStyle1}" Content="CheckBox" Grid.Row="1" Height="75" Margin="236,0,43,0"/>
 in C#, we can apply static resources like this way
C#

//For checkbox 
checkboxname.Style = (Style)this.Resources["CheckBoxStyle1"]; 
 
//For radiobutton 
 
radiobuttonname.Style = (Style)this.Resources["RadioButtonStyle1"];
4)ScreenShot


Windows Phone tutorials for beginners key points

This section is included for only windows phone beginners.However this article can covers following questions.Off course these key words is more helpful for getting more best results about this topic in search engines like google/bing/yahoo.. 

1.How to customizing radio buttons in windows phone 8 c#,xaml

2.How to customizing checkbox in windows phone 8 c#,xaml

3.c# windows phone 8 how to add style to a button/checkbox/radio button/textblock/TextBox..

4.How to add the style in windows phone 8 C#

5.How to set the style in windows phone 8 C#

6.How to set the style in windows phone 8 C#

7.Custom styles and templates in windows phone 8 C# xaml using expression blend



Have a nice day by  

2 comments:

Search Engine Submission - AddMe