c# - 除了最后一项之外,控件中的水平对齐似乎都被忽略了
问题描述
我有一个基于切换按钮的自定义单选按钮:
<Style TargetType="{x:Type local:ToolbarRadioButton}" BasedOn="{StaticResource {x:Type ToggleButton}}">
<Setter Property="Width" Value="60"/>
<Setter Property="Height" Value="60"/>
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<StackPanel Orientation="Vertical">
<iconPacks:PackIconModern Kind="{Binding TbIcon, RelativeSource={RelativeSource AncestorType=RadioButton}}"
Height="30" Width="35" HorizontalAlignment="Center"/>
<TextBlock Text="{Binding TbText, RelativeSource={RelativeSource AncestorType=RadioButton}}" FontSize="12"
HorizontalAlignment="Center"/>
</StackPanel>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
我使用这个控件来创建四个切换/单选按钮。我已经将数据模板中的项目水平居中,但我最终得到了这个:
预测及其图标似乎居中,但其他图标部分左对齐。他们都使用相同的控件,所以他们不应该都居中吗?
为清晰而编辑:无论我将它们放在哪个顺序中,预测始终是正确对齐的。文本中没有空格,图像中也没有空格,所有图像的大小都根据上面定义的控件进行调整。这是实现部分以防万一,尽管它们完全相同:
<StackPanel Orientation="Horizontal" Grid.Column="0" Grid.Row="0">
<customs:ToolbarRadioButton TbText="Day" TbIcon="CalendarDay" GroupName="calViewType" x:Name="dayOnOff"/>
<customs:ToolbarRadioButton TbText="Week" TbIcon="CalendarWeek" GroupName="calViewType" x:Name="weekOnOff"/>
<customs:ToolbarRadioButton TbText="Month" TbIcon="CalendarMonth" GroupName="calViewType" x:Name="monthOnOff" IsChecked="True"/>
<customs:ToolbarRadioButton TbText="Forecast" TbIcon="PeopleMultiple" GroupName="calViewType" x:Name="forecastOnOff"/>
</StackPanel>
解决方案
对于未来的读者:我通过深入研究 Button 的文档找到了答案。按钮上有一个名为 HorizontalContentAlignment(当然是垂直)的属性,显然需要设置它来对齐视觉内容。您不能明确设置作为内容的项目的 HorizontalAlignment 属性;显然,按钮想为你做这件事。
推荐阅读
- reactjs - 不知道如何在我的反应应用程序中将我的 api 数据显示到我的反应组件上
- python - 使用 Google Cloud DLP 存储的信息类型并获得 400 Invalid built-in info type name
- reactjs - 使用参数将路由器路由反应到错误的组件
- excel - 在 Excel 中显示重复的生日
- kotlin - 为什么显式缩小变量不会在 kotlin 中给出错误
- unity3d - 从照片中制作人物脸以实现统一
- python - 是否可以在单个列表推导中声明多个列表推导
- python - 无法在 vscode 中导入 tensorflow
- c++ - 无法获得问题的正确算法
- python - 循环遍历 pandas 数据帧并将行附加到其他数据帧,具体取决于 True/False