简体   繁体   English

除最后一项外,控件中的水平对齐似乎都被忽略了

[英]Horizontal alignment in control seems to be ignored on all but the last item

I have a custom radio button based on togglebutton: 我有一个基于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>

I use this control to create four toggle/radio buttons. 我使用此控件创建四个切换/单选按钮。 I've horizontal centered the items in datatemplate, but I end up with this: 我在datatemplate中将项目水平居中,但最终得到了以下结果:

在此处输入图片说明

Forecast and its icon seem to be centered, but the others are partially left-aligned. 预测及其图标似乎居中,而其他图标则部分向左对齐。 They all use the same control so shouldn't they all be centered? 它们都使用相同的控件,所以不都应该居中吗?

EDITS FOR CLARITY: It doesn't matter which order I put them in, Forecast is always the one that's aligned correctly. 清晰度编辑:排序顺序无关紧要,Forecast始终是正确对齐的一种。 There is no whitespace in the text nor is there whitespace in the images and all images are sized according to the control defined above. 文本中没有空格,图像中也没有空格,并且所有图像的大小均根据上面定义的控件确定。 Here's the implementation portion in case it's useful, though they're all the exact same: 这是实现部分,以防有用,尽管它们完全相同:

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

For future readers: I figured out the answer by digging into the documentation on Button. 对于将来的读者:我通过深入研究Button上的文档来找出答案。 There is a property on buttons called HorizontalContentAlignment (and vertical, of course) that apparently needs to be set to align the visual content. 按钮上有一个称为Horizo​​ntalContentAlignment(当然是垂直)的属性,显然需要设置该属性以对齐视觉内容。 You can't set the HorizontalAlignment property of the item that is the content explicitly; 您不能显式设置作为内容的项目的Horizo​​ntalAlignment属性; apparently, the button wants to do that for you. 显然,该按钮希望为您做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM