简体   繁体   English

将CommandBar(XAML)的内容居中

[英]Center the content of CommandBar (XAML)

I would like to be able to center the content of my CommandBar. 我希望能够将CommandBar的内容居中。 My current code is this 我当前的代码是这样

<Page.BottomAppBar>
    <CommandBar x:Name="BottomBar"  Background="Black" Visibility="Collapsed" OverflowButtonVisibility="Collapsed" >

        <CommandBar.PrimaryCommands>
            <AppBarButton Name="btnSend" Icon="Forward"  Foreground="White"/>
            <AppBarButton Name="btnCancel" Icon="Clear"  Foreground="White"/>
        </CommandBar.PrimaryCommands>

        <CommandBar.Content>
            <TextBlock Name="tbBar" Text="EOOOOOOOOO" Foreground="White" Margin="12,14" VerticalAlignment="Center" HorizontalAlignment="Center"/>
        </CommandBar.Content>
    </CommandBar>

</Page.BottomAppBar>

This puts the content on the left and aligned horizontally and vertically but I need it in the center. 这会将内容放在左侧,并在水平和垂直方向上对齐,但我需要在中间。

I have written about this on my blog here , so browse it for full detail. 我已经在这里的博客上写过有关此内容的文章,因此请浏览它以获取详细信息。 The reason for this problem is the IsDynamicOverflowEnabled property, which was added in the Anniversary Update. 出现此问题的原因是在周年更新中添加的IsDynamicOverflowEnabled属性。 When this is set to enabled, the Grid Column the Content of the CommandBar resides in gets Width="Auto" , which means it gives the content only the space it actually needs and reserves all remaining space to the CommandBar buttons. 设置为启用时, CommandBar Content所在的Grid列将获得Width="Auto" ,这意味着它将仅向内容提供其实际需要的空间,并为CommandBar按钮保留所有剩余空间。

The easiest solution is to set IsDynamicOverflowEnabled to false. 最简单的解决方案是将IsDynamicOverflowEnabled设置为false。 If you want to keep this feature, you would have to modify the default template of CommandBar . 如果要保留此功能,则必须修改CommandBar的默认模板。

<CommandBar HorizontalContentAlignment="Center"
            IsDynamicOverflowEnabled="False">
   <CommandBar.Content>
      <TextBlock Text="Content" />
   </CommandBar.Content>
</CommandBar>

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

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