You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WPF .NET 6:StackPanel内应用MenuButtonTheme的RadioButton不显示

WPF RadioButton应用自定义样式后无法显示的解决方法

问题说明

首次使用WPF,遇到应用了MenuButtonTheme样式的RadioButton无法在StackPanel中垂直显示的问题,预期按钮能正常垂直排列展示。

相关代码

主窗口XAML代码

<Window x:Class="GetraenkespenderDesign.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:GetraenkespenderDesign"
        mc:Ignorable="d"
        Height="600" Width="920"
        WindowStyle ="None"
        ResizeMode="NoResize"
        Background="Transparent"
        AllowsTransparency="True">
    <Border Background="#272537"
            CornerRadius="20">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="200">
                </ColumnDefinition>
                <ColumnDefinition Width="0"/>
            </Grid.ColumnDefinitions>

            <Grid.RowDefinitions>
                <RowDefinition Height="75">
                </RowDefinition>
                <RowDefinition Height="0"/>
            </Grid.RowDefinitions>

            <TextBlock Text="Elysian"
                       VerticalAlignment="Center"
                       HorizontalAlignment="Left"
                       Foreground="White"
                       FontSize="22"
                       Margin="20,0,0,0"/>

            <StackPanel Grid.Row="1"
                        Orientation="Vertical">
                <RadioButton Content="Home"
                             Height="50"
                             Foreground="White"
                             FontSize="14"
                             Style="{StaticResource MenuButtonTheme}"/>
                <RadioButton Content="Discovery"
                             Height="50"
                             Foreground="White"
                             FontSize="14"
                             Style="{StaticResource MenuButtonTheme}"/>
                <RadioButton Content="Feature"
                             Height="50"
                             Foreground="White"
                             FontSize="14"
                             Style="{StaticResource MenuButtonTheme}"/>
            </StackPanel>
        </Grid>
    </Border>
</Window>
<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}">
                            <TextBlock Text="{TemplateBinding Property=ContentStringFormat}"
                                       VerticalAlignment="Center"
                                       Margin="50,0,0,0"/>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>

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

        <Style.Triggers>
            <Trigger Property="IsChecked" Value="True">
                <Setter Property="Background" Value="#22202f"/>
            </Trigger>
        </Style.Triggers>
    </Style>
</ResourceDictionary>

问题修复

1. 调整Grid行高度

主窗口中Grid的第二行高度被设为0,导致StackPanel没有显示空间。修改Grid.RowDefinitions:

<Grid.RowDefinitions>
    <RowDefinition Height="75"/>
    <RowDefinition Height="*"/> <!-- 改为占用剩余全部空间 -->
</Grid.RowDefinitions>

2. 修正样式中的文本绑定

样式里的TextBlock错误绑定了ContentStringFormat(这是用于格式化内容的属性,并非直接绑定按钮文本),同时需要继承Foreground和FontSize属性,确保样式生效:

<TextBlock Text="{TemplateBinding Content}"
           VerticalAlignment="Center"
           Margin="50,0,0,0"
           Foreground="{TemplateBinding Foreground}"
           FontSize="{TemplateBinding FontSize}"/>

3. 可选:修正Style的BasedOn

RadioButton的样式基类应基于RadioButton默认样式,而非ToggleButton,避免潜在冲突:

<Style BasedOn="{StaticResource {x:Type RadioButton}}"
       TargetType="{x:Type RadioButton}"
       x:Key="MenuButtonTheme">

内容的提问来源于stack exchange,提问作者Dustin Derr

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 14:54:55