Search code examples
xamarin.formsmauimaui-community-toolkit

.NET Maui Create a ControlTemplate Programmatically


I hope you can help me since I can't find much information about it, I need to redefine the appearance of the radioButtons, for which I already have the XAML controlTemplate and it works fine. But I need to create a contentView by code (CS) I can't find the way to pass the XAML to c#

<ControlTemplate x:Key="RadioButtonTemplate">
            <Border Stroke="#F3F2F1"
                    StrokeThickness="0"
                    StrokeShape="RoundRectangle 10"
                    BackgroundColor="Transparent"
                    HorizontalOptions="Start"
                    VerticalOptions="Start">
                <VisualStateManager.VisualStateGroups>
                    <VisualStateGroupList>
                        <VisualStateGroup x:Name="CheckedStates">
                            <VisualState x:Name="Checked">
                                <VisualState.Setters>
                                    <Setter Property="Stroke"
                                            Value="#FF3300" />
                                    <Setter TargetName="check"
                                            Property="Opacity"
                                            Value="1" />
                                </VisualState.Setters>
                            </VisualState>
                            <VisualState x:Name="Unchecked">
                                <VisualState.Setters>
                                    <Setter Property="BackgroundColor"
                                            Value="Transparent" />
                                    <Setter Property="Stroke"
                                            Value="#F3F2F1" />
                                    <Setter TargetName="check"
                                            Property="Opacity"
                                            Value="0" />
                                </VisualState.Setters>
                            </VisualState>
                        </VisualStateGroup>
                    </VisualStateGroupList>
                </VisualStateManager.VisualStateGroups>
                <Grid Margin="5">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="auto" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>
                    <Grid Grid.Column="0" Margin="0,0,4,0"
                              WidthRequest="18"
                              HeightRequest="18"
                              HorizontalOptions="End"
                              VerticalOptions="Start">
                        <Ellipse Stroke="#63BBCD"
                                     Fill="White"
                                     WidthRequest="16"
                                     HeightRequest="16"
                                     HorizontalOptions="Center"
                                     VerticalOptions="Center" />
                        <Ellipse x:Name="check"
                                     Fill="#63BBCD"
                                     WidthRequest="16"
                                     HeightRequest="16"
                                     HorizontalOptions="Center"
                                     VerticalOptions="Center" />
                    </Grid>
                    <ContentPresenter Grid.Column="1" />

                </Grid>
            </Border>
        </ControlTemplate>
        <Style TargetType="RadioButton">
            <Setter Property="ControlTemplate"
                    Value="{StaticResource RadioButtonTemplate}" />
        </Style>

Any help is greatly appreciated


Solution

  • It's actually easier than you might think

    You would first need to get the Resource as an object then cast it as ControlTemplate then assign it to the ContentView of choice.

      // if your resource is local 
      var rbTemplate = (ControlTemplate)this.Resources["RadioButtonTemplate"];
      //if your resource is global 
      var rbTemplate = (ControlTemplate)Application.Current.Resources["RadioButtonTemplate"];
      contentView.ControlTemplate = rbTemplate;