C#中RadioButton内容过长需换行的问题求助(附XAML代码)
问题根源
你遇到的问题有两个核心点:
- 直接给RadioButton的
Content属性设置带
的字符串不会生效,因为默认的内容呈现器不解析换行符 - 你给RadioButton固定了
Height="20",就算内容能换行,高度也不够显示多行文本
两种可行解决方案
方案1:自动换行(推荐)
把RadioButton的Content换成TextBlock,开启自动换行,同时去掉固定高度(或设为Auto):
<RadioButton x:Name="RbMitarbeiterNicht" Grid.ColumnSpan="2" HorizontalAlignment="Left" Margin="863,929,0,0" VerticalAlignment="Top" GroupName="rb_stoerung" FontSize="16" Width="102" Foreground="#000000" Background="#FFFFFF" Checked="RbMitarbeiterNicht_Checked"> <TextBlock Text="Mitarbeiter nicht anwesend" TextWrapping="Wrap"/> </RadioButton>
这样文本会根据RadioButton的宽度自动换行,控件高度也会自适应内容。
方案2:手动指定换行位置
如果需要在特定位置换行,在TextBlock的文本里插入
(或者直接在后台代码里用\n):
<RadioButton x:Name="RbMitarbeiterNicht" Grid.ColumnSpan="2" HorizontalAlignment="Left" Margin="863,929,0,0" VerticalAlignment="Top" GroupName="rb_stoerung" FontSize="16" Width="102" Foreground="#000000" Background="#FFFFFF" Checked="RbMitarbeiterNicht_Checked"> <TextBlock Text="Mitarbeiter nicht
anwesend" TextWrapping="Wrap"/> </RadioButton>
关键注意事项
- 必须去掉固定的
Height="20",否则多行内容会被截断 - 不要直接给RadioButton的
Content属性设置带换行的字符串,必须用TextBlock这类支持换行的控件承载内容
内容的提问来源于stack exchange,提问作者Code123
相关产品推荐
相关产品推荐

