我一直在尝试各种技术来尝试使按钮标签文本中心对齐。当我查看Avalonia DevTools探矿器中的按钮时,我可以看到AccessText TextAlignment总是设置为左。
下面是应用样式的一次尝试,但不起作用:
<Style Selector="Button">
<Setter Property="Background" Value="Black"/>
<Setter Property="Foreground" Value="Gray"/>
<Setter Property="FontWeight" Value="Bold" />
<Setter Property="HorizontalAlignment" Value="Center" />
<Setter Property="TextBlock.TextAlignment" Value="Center"/>
</Style>修改TextBlock TextAlignment的正确方法是什么?
发布于 2022-02-24 21:00:49
有两种不同的对齐方式:
中的内容
若要在父元素中水平对齐元素,可以使用HorizontalAlignment。但是,要对齐内容,必须使用HorizontalContentAlignment。
在布局可视化器区域右侧的Avalonia工具中显示的是HorizontalAlignment。但是,当按钮被选中时,您可以在文本形式的Properties区域的中间看到HorizontalContentAlignment。
最好用一个简短的XAML示例演示这些不同的对齐:
alignment
的样式的按钮。
<Window.Styles>
<Style Selector="Button#myBtn">
<Setter Property="Background" Value="Black" />
<Setter Property="Foreground" Value="Gray" />
<Setter Property="FontWeight" Value="Bold" />
<Setter Property="HorizontalContentAlignment" Value="Center" />
</Style>
</Window.Styles>
<StackPanel Background="#D9D0DE">
<!-- different horizontal alignments of element in parent panel -->
<StackPanel Background="#C46D5E">
<Button HorizontalAlignment="Left" Content="Left" Width="200" />
<Button HorizontalAlignment="Center" Content="Center" Width="200" />
<Button HorizontalAlignment="Right" Content="Right" Width="200" />
</StackPanel>
<!-- different horizontal content alignments -->
<StackPanel Background="#7A9E7E">
<Button HorizontalContentAlignment="Left" Content="Content Left" Width="200" />
<Button HorizontalContentAlignment="Center" Content="Content Center" Width="200" />
<Button HorizontalContentAlignment="Right" Content="Content Right" Width="200" />
</StackPanel>
<!-- styled button -->
<StackPanel>
<Button x:Name="myBtn" Content="Styled Button" Width="200" />
</StackPanel>
</StackPanel>从视觉上看,它看起来像这样:

正如您在底部按钮上看到的,样式以水平居中的方式对齐按钮内容。
https://stackoverflow.com/questions/71146760
复制相似问题