wpf 为isChecked上带有文本的单选按钮添加边框

92vpleto  于 2023-01-06  发布在  其他
关注(0)|答案(1)|浏览(179)

我想为isChecked上带有文本的单选按钮添加边框
我尝试了Style.Trigger,但它似乎没有操作。还尝试了通过ControlTemplate.Trigger,但同时引用网格和文本框时,显示无法识别参数

<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>
u3r8eeie

u3r8eeie1#

TextBlock没有边框,因此也无法为边框设置值。
它可以用两种方法来解决。
1.在只读模式下使用TextBox代替TextBlock元素。

<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>

1.由于面板中只有一个元素,因此可以改用“边框”。

<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>

相关问题