RadioButton whith Button style XAML

前端 未结 2 1791
孤街浪徒
孤街浪徒 2021-01-19 07:20

How can I change the radiobutton style to a button style in XAML? And how to set the background color if the radiobutton is checked? I would like to use the default colors (

相关标签:
2条回答
  • 2021-01-19 07:52

    You need to define the controltemplate for RadioButton and apply a trigger in controltemplate. Something like.

    <Style x:Key="ButtonRadioButtonStyle" TargetType="{x:Type RadioButton}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type RadioButton}">
                        <Button Content="{TemplateBinding Content}"/>
                        <ControlTemplate.Triggers>
                            <Trigger Property="HasContent" Value="true">
                                <Setter Property="FocusVisualStyle" Value="{StaticResource CheckRadioFocusVisual}"/>
                                <Setter Property="Padding" Value="4,0,0,0"/>
                            </Trigger>
                            <Trigger Property="IsEnabled" Value="false">
                                <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                            </Trigger>
                            <Trigger Property="IsChecked" Value="false">
                                <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    

    And use it like.

    <RadioButton Style="{DynamicResource ButtonRadioButtonStyle}">
                <Image Source="ImagePath\ABC.png"/>
            </RadioButton>
    

    Hope it helps..

    0 讨论(0)
  • 2021-01-19 07:57

    I often use this option

    <RadioButton Height="23" Width="23" ToolTip="По левому краю" GroupName="TextAlignmentGroup"
                                                    IsChecked="{Binding IsChecked}">
        <RadioButton.Template>
            <ControlTemplate TargetType="RadioButton">
                <Image Name="ImageName" Stretch="Fill"/>
                <ControlTemplate.Triggers>
                    <Trigger Property="ToggleButton.IsChecked" Value="True">
                        <Setter TargetName="ImageName" Property="Image.Source">
                            <Setter.Value>
                                <BitmapImage UriSource="../../Resources/pressed.png"></BitmapImage>
                            </Setter.Value>
                        </Setter>
                    </Trigger>
                    <Trigger Property="ToggleButton.IsChecked" Value="False">
                        <Setter TargetName="ImageName" Property="Image.Source">
                            <Setter.Value>
                                <BitmapImage UriSource="../../Resources/image.png"></BitmapImage>
                            </Setter.Value>
                        </Setter>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </RadioButton.Template>
    </RadioButton>
    
    0 讨论(0)
提交回复
热议问题