Home > Back-end >  Add border to radioButton with text on isChecked
Add border to radioButton with text on isChecked

Time:01-04

I want to add border to radiobutton with text on isChecked

I tried Style.Trigger but it didn't seem to have action Also tried via ControlTemplate.Trigger but referencing to both grid and text box says Parameter is not recognized

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style BasedOn="{StaticResource {x:Type ToggleButton}}"
       TargetType="{x:Type RadioButton}"
       x:Key="MenuButtonTheme">

    <Style.Setters>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="RadioButton">
                    <Grid VerticalAlignment="Stretch"
                                HorizontalAlignment="Stretch"
                                Background="{TemplateBinding Background}"
                                x:Name="GridMain">
                        <TextBlock Text="{TemplateBinding Property=Content}"
                                   x:Name="ButtonText"
                                VerticalAlignment="Center"
                                Margin="20,0,0,0">

                        </TextBlock>
                    </Grid>

                    <ControlTemplate.Triggers>
                        <Trigger Property="IsChecked" Value="True">
                            <Setter TargetName="ButtonText" Property="Margin" Value="30,0,0,0"></Setter>
                            <!-- <Setter TargetName="ButtonText" Property="BorderThickness" Value="5"></Setter> -->
                            <!-- <Setter TargetName="GridMain" Property="BorderThickness" Value="5"></Setter> -->
                        </Trigger>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter TargetName="GridMain" Property="Background" Value="#424549"></Setter>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>

        </Setter>
        <Setter Property="Background" Value="Transparent"></Setter>
    </Style.Setters>

    <Style.Triggers>
        <Trigger Property="IsChecked" Value="True">
            <Setter Property="BorderThickness" Value="5"></Setter>
            <Setter Property="BorderBrush" Value="#7289da"></Setter>
        </Trigger>
    </Style.Triggers>
    
</Style>

CodePudding user response:

The TextBlock doesn't have a border, so it's impossible to set a value for the border either.
It can be solved in two ways.

  1. Use instead of TextBlock element TextBox in read-only mode.

             <ControlTemplate TargetType="RadioButton">
                 <Grid VerticalAlignment="Stretch"
                             HorizontalAlignment="Stretch"
                             Background="{TemplateBinding Background}"
                             x:Name="GridMain">
                     <TextBox Text="{TemplateBinding Content}"
                              x:Name="ButtonText"
                              VerticalAlignment="Center"
                              Margin="20,0,0,0"
                              IsReadOnly="True"/>
                 </Grid>
    
                 <ControlTemplate.Triggers>
                     <Trigger Property="IsChecked" Value="True">
                         <Setter TargetName="ButtonText" Property="Margin" Value="30,0,0,0"></Setter>
                         <Setter TargetName="ButtonText" Property="BorderThickness" Value="5"/>
                     </Trigger>
                     <Trigger Property="IsMouseOver" Value="True">
                         <Setter TargetName="GridMain" Property="Background" Value="#424549"></Setter>
                     </Trigger>
                 </ControlTemplate.Triggers>
             </ControlTemplate>
    
  2. Since you have only one element in the panel, you can use Border instead.

             <ControlTemplate TargetType="RadioButton">
                 <Border VerticalAlignment="Stretch"
                         HorizontalAlignment="Stretch"
                         Background="{TemplateBinding Background}"
                         x:Name="GridMain">
                     <TextBlock Text="{TemplateBinding Property=Content}"
                                x:Name="ButtonText"
                             VerticalAlignment="Center"
                             Margin="20,0,0,0">
    
                     </TextBlock>
                 </Border>
    
                 <ControlTemplate.Triggers>
                     <Trigger Property="IsChecked" Value="True">
                         <Setter TargetName="ButtonText" Property="Margin" Value="30,0,0,0"></Setter>
                         <Setter TargetName="GridMain" Property="BorderThickness" Value="5"/>
                     </Trigger>
                     <Trigger Property="IsMouseOver" Value="True">
                         <Setter TargetName="GridMain" Property="Background" Value="#424549"></Setter>
                     </Trigger>
                 </ControlTemplate.Triggers>
             </ControlTemplate>
    
  • Related