首页 文章

控制中的水平对齐似乎在除最后一项之外的所有项目上都被忽略

提问于
浏览
1

我有一个基于togglebutton的自定义单选按钮:

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

我使用此控件创建四个切换/单选按钮 . 我对datatemplate中的项目进行了水平居中,但我最终得到了这个:

预测及其图标似乎居中,但其他部分左对齐 . 它们都使用相同的控件,所以它们不应该都集中在一起吗?

澄清的编辑:我把它们放在哪个顺序并不重要,预测始终是正确对齐的 . 文本中没有空格,图像中也没有空白,所有图像都根据上面定义的控件确定大小 . 这是实现部分,只要它有用,尽管它们完全相同:

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

1 回答

  • 2

    对于未来的读者:我通过深入研究Button的文档找到了答案 . 按钮上有一个名为HorizontalContentAlignment的属性(当然也是垂直的),显然需要设置它来对齐可视内容 . 您不能显式设置作为内容的项的HorizontalAlignment属性;显然,按钮想要为你做这件事 .

相关问题