【问题标题】:UWP TextBox VerticalContentAlignmentUWP 文本框 VerticalContentAlignment
【发布时间】:2016-07-19 22:04:09
【问题描述】:

我遇到了以下问题:我无法垂直对齐 TextBox 内的内容。我旁边有用于用户输入的图像图标和 TextBox,但 TextBox 中的文本具有默认的顶部对齐方式。我可以通过设置VerticalAlignment="Center" 来更改它,但是TextBox 的高度变得比我需要的要小(它应该有43px 的高度)。我试过VerticalContentAlignment="Center,但它也不起作用。我只知道一种可能的解决方案:在 TextBox 中设置 padding,但我不喜欢这个想法。也许还有其他我不知道的解决方案?

提前致谢!

这是我的部分代码:

<Grid Margin="15,0,15,10">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <Image Grid.Column="0" 
                       Source="/Assets/TextInputIcons/Money.png" 
                       Style="{StaticResource InputFieldIconsStyle}" />
                <TextBox Grid.Column="1" 
                         PlaceholderText="Sum" 
                         Style="{StaticResource NumberedTextBox}" />
            </Grid>

和示例图片:

【问题讨论】:

  • 编辑文本框样式并尝试
  • 您的意思是编辑默认文本框样式?

标签: xaml windows-phone win-universal-app universal


【解决方案1】:

试试:

<TextBlock Text="Text" VerticalAlignment="Center" TextLineBounds="TrimToCapHeight"/>

<TextBlock Text="Text" VerticalAlignment="Center" TextLineBounds="Tight"/>

【讨论】:

    【解决方案2】:

    您必须编辑默认模板才能使其正常工作,您会注意到那里有一个ScrollViewer,如果您设置了VerticalAlignment="True",那么它应该可以解决问题,但是,我注意到我的其中一个行为非常奇怪由于某种原因,内容一直在疯狂闪烁的文本框。

    因为无论如何我都不需要ScrollViewer,而您似乎也不需要,所以我只是用ContentPresenter 替换它,然后设置它的VerticalContentAlignment="True"

    您可以查看Sapan's answer 以查看默认样式的外观,采用该样式,将ScrollViewer 替换为ContentPresenter,删除损坏的XAML,然后添加VerticalContentAlignment="True"

    看起来像这样:

    <ContentPresenter x:Name="ContentElement" Grid.Column="1" Grid.Row="1" 
                      AutomationProperties.AccessibilityView="Raw" 
                      Margin="{TemplateBinding BorderThickness}" 
                      Padding="{TemplateBinding Padding}" 
                      VerticalContentAlignment="Center"/>
    

    你会认为这会更容易完成......

    【讨论】:

      【解决方案3】:

      可以通过创建如下样式轻松完成:

      • 右键单击文本框,编辑模板 => 创建副本。
      • 在生成样式的底部,查找 (ScrollViewer x:Name="ContentElement") 的内容和 (ContentControl x:Name="PlaceholderTextContentPresenter")用于文本占位符。将 (VerticalAlignment="Center") 添加到它们。

      例子:

      <Page.Resources>
              <Style x:Key="CenterTextBoxStyle" TargetType="TextBox">
                  <Setter Property="MinWidth" Value="{ThemeResource TextControlThemeMinWidth}"/>
                  <Setter Property="MinHeight" Value="{ThemeResource TextControlThemeMinHeight}"/>
                  <Setter Property="Foreground" Value="{ThemeResource SystemControlForegroundBaseHighBrush}"/>
                  <Setter Property="Background" Value="{ThemeResource SystemControlBackgroundAltHighBrush}"/>
                  <Setter Property="BorderBrush" Value="{ThemeResource SystemControlForegroundChromeDisabledLowBrush}"/>
                  <Setter Property="SelectionHighlightColor" Value="{ThemeResource SystemControlHighlightAccentBrush}"/>
                  <Setter Property="BorderThickness" Value="{ThemeResource TextControlBorderThemeThickness}"/>
                  <Setter Property="FontFamily" Value="{ThemeResource ContentControlThemeFontFamily}"/>
                  <Setter Property="FontSize" Value="{ThemeResource ControlContentThemeFontSize}"/>
                  <Setter Property="ScrollViewer.HorizontalScrollMode" Value="Auto"/>
                  <Setter Property="ScrollViewer.VerticalScrollMode" Value="Auto"/>
                  <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Hidden"/>
                  <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Hidden"/>
                  <Setter Property="ScrollViewer.IsDeferredScrollingEnabled" Value="False"/>
                  <Setter Property="Padding" Value="{ThemeResource TextControlThemePadding}"/>
                  <Setter Property="Template">
                      <Setter.Value>
                          <ControlTemplate TargetType="TextBox">
                              <Grid>
                                  <Grid.Resources>
                                      <Style x:Name="DeleteButtonStyle" TargetType="Button">
                                          <Setter Property="Template">
                                              <Setter.Value>
                                                  <ControlTemplate TargetType="Button">
                                                      <Grid x:Name="ButtonLayoutGrid" BorderBrush="{ThemeResource TextBoxButtonBorderThemeBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{ThemeResource TextBoxButtonBackgroundThemeBrush}">
                                                          <VisualStateManager.VisualStateGroups>
                                                              <VisualStateGroup x:Name="CommonStates">
                                                                  <VisualState x:Name="Normal"/>
                                                                  <VisualState x:Name="PointerOver">
                                                                      <Storyboard>
                                                                          <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="GlyphElement">
                                                                              <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightAccentBrush}"/>
                                                                          </ObjectAnimationUsingKeyFrames>
                                                                      </Storyboard>
                                                                  </VisualState>
                                                                  <VisualState x:Name="Pressed">
                                                                      <Storyboard>
                                                                          <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="ButtonLayoutGrid">
                                                                              <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightAccentBrush}"/>
                                                                          </ObjectAnimationUsingKeyFrames>
                                                                          <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="GlyphElement">
                                                                              <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightAltChromeWhiteBrush}"/>
                                                                          </ObjectAnimationUsingKeyFrames>
                                                                      </Storyboard>
                                                                  </VisualState>
                                                                  <VisualState x:Name="Disabled">
                                                                      <Storyboard>
                                                                          <DoubleAnimation Duration="0" To="0" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="ButtonLayoutGrid"/>
                                                                      </Storyboard>
                                                                  </VisualState>
                                                              </VisualStateGroup>
                                                          </VisualStateManager.VisualStateGroups>
                                                          <TextBlock x:Name="GlyphElement" AutomationProperties.AccessibilityView="Raw" Foreground="{ThemeResource SystemControlForegroundChromeBlackMediumBrush}" FontStyle="Normal" FontSize="12" FontFamily="{ThemeResource SymbolThemeFontFamily}" HorizontalAlignment="Center" Text="&#xE10A;" VerticalAlignment="Center"/>
                                                      </Grid>
                                                  </ControlTemplate>
                                              </Setter.Value>
                                          </Setter>
                                      </Style>
                                  </Grid.Resources>
                                  <Grid.ColumnDefinitions>
                                      <ColumnDefinition Width="*"/>
                                      <ColumnDefinition Width="Auto"/>
                                  </Grid.ColumnDefinitions>
                                  <Grid.RowDefinitions>
                                      <RowDefinition Height="Auto"/>
                                      <RowDefinition Height="*"/>
                                  </Grid.RowDefinitions>
                                  <VisualStateManager.VisualStateGroups>
                                      <VisualStateGroup x:Name="CommonStates">
                                          <VisualState x:Name="Disabled">
                                              <Storyboard>
                                                  <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="HeaderContentPresenter">
                                                      <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlDisabledBaseMediumLowBrush}"/>
                                                  </ObjectAnimationUsingKeyFrames>
                                                  <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="BackgroundElement">
                                                      <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlDisabledTransparentBrush}"/>
                                                  </ObjectAnimationUsingKeyFrames>
                                                  <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="BorderElement">
                                                      <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlBackgroundBaseLowBrush}"/>
                                                  </ObjectAnimationUsingKeyFrames>
                                                  <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="BorderElement">
                                                      <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlDisabledBaseLowBrush}"/>
                                                  </ObjectAnimationUsingKeyFrames>
                                                  <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentElement">
                                                      <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlDisabledChromeDisabledLowBrush}"/>
                                                  </ObjectAnimationUsingKeyFrames>
                                                  <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="PlaceholderTextContentPresenter">
                                                      <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlDisabledChromeDisabledLowBrush}"/>
                                                  </ObjectAnimationUsingKeyFrames>
                                              </Storyboard>
                                          </VisualState>
                                          <VisualState x:Name="Normal"/>
                                          <VisualState x:Name="PointerOver">
                                              <Storyboard>
                                                  <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="BorderElement">
                                                      <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightChromeAltLowBrush}"/>
                                                  </ObjectAnimationUsingKeyFrames>
                                                  <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Opacity" Storyboard.TargetName="BackgroundElement">
                                                      <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource TextControlBackgroundHoverOpacity}"/>
                                                  </ObjectAnimationUsingKeyFrames>
                                              </Storyboard>
                                          </VisualState>
                                          <VisualState x:Name="Focused">
                                              <Storyboard>
                                                  <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="PlaceholderTextContentPresenter">
                                                      <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlPageTextChromeBlackMediumLowBrush}"/>
                                                  </ObjectAnimationUsingKeyFrames>
                                                  <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="BackgroundElement">
                                                      <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlBackgroundChromeWhiteBrush}"/>
                                                  </ObjectAnimationUsingKeyFrames>
                                                  <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Opacity" Storyboard.TargetName="BackgroundElement">
                                                      <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource TextControlBackgroundFocusedOpacity}"/>
                                                  </ObjectAnimationUsingKeyFrames>
                                                  <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="BorderElement">
                                                      <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightAccentBrush}"/>
                                                  </ObjectAnimationUsingKeyFrames>
                                                  <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentElement">
                                                      <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlForegroundChromeBlackHighBrush}"/>
                                                  </ObjectAnimationUsingKeyFrames>
                                                  <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="RequestedTheme" Storyboard.TargetName="ContentElement">
                                                      <DiscreteObjectKeyFrame KeyTime="0" Value="Light"/>
                                                  </ObjectAnimationUsingKeyFrames>
                                              </Storyboard>
                                          </VisualState>
                                      </VisualStateGroup>
                                      <VisualStateGroup x:Name="ButtonStates">
                                          <VisualState x:Name="ButtonVisible">
                                              <Storyboard>
                                                  <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="DeleteButton">
                                                      <DiscreteObjectKeyFrame KeyTime="0">
                                                          <DiscreteObjectKeyFrame.Value>
                                                              <Visibility>Visible</Visibility>
                                                          </DiscreteObjectKeyFrame.Value>
                                                      </DiscreteObjectKeyFrame>
                                                  </ObjectAnimationUsingKeyFrames>
                                              </Storyboard>
                                          </VisualState>
                                          <VisualState x:Name="ButtonCollapsed"/>
                                      </VisualStateGroup>
                                  </VisualStateManager.VisualStateGroups>
                                  <Border x:Name="BackgroundElement" Background="{TemplateBinding Background}" Grid.ColumnSpan="2" Margin="{TemplateBinding BorderThickness}" Opacity="{ThemeResource TextControlBackgroundRestOpacity}" Grid.Row="1" Grid.RowSpan="1"/>
                                  <Border x:Name="BorderElement" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Grid.ColumnSpan="2" Grid.Row="1" Grid.RowSpan="1"/>
                                  <ContentPresenter x:Name="HeaderContentPresenter" Grid.ColumnSpan="2" ContentTemplate="{TemplateBinding HeaderTemplate}" Content="{TemplateBinding Header}" Foreground="{ThemeResource SystemControlForegroundBaseHighBrush}" FontWeight="Normal" Margin="0,0,0,8" Grid.Row="0" Visibility="Collapsed" x:DeferLoadStrategy="Lazy"/>
                                  <ScrollViewer x:Name="ContentElement" VerticalAlignment="Center" AutomationProperties.AccessibilityView="Raw" HorizontalScrollMode="{TemplateBinding ScrollViewer.HorizontalScrollMode}" HorizontalScrollBarVisibility="{TemplateBinding ScrollViewer.HorizontalScrollBarVisibility}" IsTabStop="False" IsHorizontalRailEnabled="{TemplateBinding ScrollViewer.IsHorizontalRailEnabled}" IsVerticalRailEnabled="{TemplateBinding ScrollViewer.IsVerticalRailEnabled}" IsDeferredScrollingEnabled="{TemplateBinding ScrollViewer.IsDeferredScrollingEnabled}" Margin="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}" Grid.Row="1" VerticalScrollBarVisibility="{TemplateBinding ScrollViewer.VerticalScrollBarVisibility}" VerticalScrollMode="{TemplateBinding ScrollViewer.VerticalScrollMode}" ZoomMode="Disabled"/>
                                  <ContentControl x:Name="PlaceholderTextContentPresenter" VerticalAlignment="Center" Grid.ColumnSpan="2" Content="{TemplateBinding PlaceholderText}" Foreground="{ThemeResource SystemControlPageTextBaseMediumBrush}" IsHitTestVisible="False" IsTabStop="False" Margin="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}" Grid.Row="1"/>
                                  <Button x:Name="DeleteButton" BorderThickness="{TemplateBinding BorderThickness}" Grid.Column="1" FontSize="{TemplateBinding FontSize}" IsTabStop="False" Margin="{ThemeResource HelperButtonThemePadding}" MinWidth="34" Grid.Row="1" Style="{StaticResource DeleteButtonStyle}" Visibility="Collapsed" VerticalAlignment="Stretch"/>
                              </Grid>
                          </ControlTemplate>
                      </Setter.Value>
                  </Setter>
              </Style>
          </Page.Resources>
      

      这样 TextPlaceholderText 始终保持在中心,无论 TextBox 的高度或字体如何。

      【讨论】:

        【解决方案4】:

        我有和你一样的问题,我知道是字体大小错误。TextBox可以让它的高度确定字体大小,但是你设置VerticalContentAlignment="Center" 我有一个解决方案,可以制作边框

            <Border Grid.Column="1">
                <TextBox/>
            </Border>
        

        它的BorderThickness 作为TextBox 的BorderThickness 和它的画笔作为TextBox 的画笔。 你设置了 TextBox VerticalAlignment="Center"BorderThickness ="0"

        【讨论】:

          猜你喜欢
          • 2011-10-08
          • 1970-01-01
          • 2019-05-09
          • 2019-01-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-03-19
          • 1970-01-01
          相关资源
          最近更新 更多