垂直文本对齐在UWP中的RichEditBox。

huangapple go评论48阅读模式
英文:

Vertical Text alignment for RichEditBox in uwp

问题

如何在UWP中垂直对齐RichEditBox中的文本,我阅读了整个文档但找不到如何实现的方法。

    public sealed partial class MainPage : Page
    {
        RichEditBox box1;
        public MainPage()
        {
            this.InitializeComponent();
            box1 = new RichEditBox()
            {
                Width = 500,
                Height = 500,
                BorderBrush = new SolidColorBrush(Colors.Blue),
                BorderThickness = new Thickness(1, 1, 1, 1)
            };
            box1.Document.SetText(0, "asdfghjklkjhgfdsgffffffffffff\nzAsdfghjk\nlkjhgfdsasdfgh\nkjhgfdfghjkl\nlkjhgfdghjk");
            box1.TextAlignment = TextAlignment.Center;

            Content = box1;
        }
}

如图所示,我能够水平对齐文本,但无法垂直对齐文本。
垂直文本对齐在UWP中的RichEditBox。

下面是在Excel表格中实现垂直对齐和水平对齐的示例
垂直文本对齐在UWP中的RichEditBox。

英文:

How to vertically align the text in the RichEditBox in uwp, I've read the whole documentation but could not found how to achieve that.

    public sealed partial class MainPage : Page
    {
        RichEditBox box1;
        public MainPage()
        {
            this.InitializeComponent();
            box1 = new RichEditBox()
            {
                Width = 500,
                Height = 500,
                BorderBrush = new SolidColorBrush(Colors.Blue),
                BorderThickness = new Thickness(1, 1, 1, 1)
            };
            box1.Document.SetText(0, "asdfghjklkjhgfdsgffffffffffff\nzAsdfghjk\nlkjhgfdsasdfgh\nkjhgfdfghjkl\nlkjhgfdghjk");
            box1.TextAlignment = TextAlignment.Center;

            Content = box1;
        }
}

As shown in the image I was able to align the text horizontally but not able to align the text vertically.
垂直文本对齐在UWP中的RichEditBox。

Below is the example how in excel sheet they achieved the vertical align and horizontal align both
垂直文本对齐在UWP中的RichEditBox。

答案1

得分: 1

目前,有内置的 API 可以直接更改文本的垂直对齐方式。TextAlignment 属性只能更改水平对齐方式。

你需要更改 RichEditBox 的默认样式以获得你想要的效果。

  1. 创建 RichEditBox 的默认样式。

  2. 找到样式内名为 ContentElementScrollViewer 对象。这是文本放置的地方。

  3. ContentElementVerticalAlignment 属性设置为 Center,文本将垂直居中放置。

以下是我用于测试的 XAML 样式:

<Style x:Key="RichEditBoxStyle1" TargetType="RichEditBox">
  ...
</Style>

结果如图所示:
垂直文本对齐在UWP中的RichEditBox。

英文:

Currently, there is built-in API that could directly change the VerticalAlignment of the text. The TextAlignment property could only change the HorizontalAlignment.

You have to change the default style of the RichEditBox to get what you want.

  1. Create a default style of the RichEditBox

  2. Find a ScrollViewer object which named ContentElement inside the style. It's the place where text are placed.

  3. Set the VerticalAlignment property of the ContentElement to Center and the text will be placed in the center Vertically.

The following XAML style is what I used for test:

        &lt;Style x:Key=&quot;RichEditBoxStyle1&quot; TargetType=&quot;RichEditBox&quot;&gt;
&lt;Setter Property=&quot;Foreground&quot; Value=&quot;{ThemeResource TextControlForeground}&quot;/&gt;
&lt;Setter Property=&quot;Background&quot; Value=&quot;{ThemeResource TextControlBackground}&quot;/&gt;
&lt;Setter Property=&quot;ContentLinkForegroundColor&quot; Value=&quot;{ThemeResource ContentLinkForegroundColor}&quot;/&gt;
&lt;Setter Property=&quot;ContentLinkBackgroundColor&quot; Value=&quot;{ThemeResource ContentLinkBackgroundColor}&quot;/&gt;
&lt;Setter Property=&quot;SelectionHighlightColor&quot; Value=&quot;{ThemeResource TextControlSelectionHighlightColor}&quot;/&gt;
&lt;Setter Property=&quot;BorderBrush&quot; Value=&quot;{ThemeResource TextControlBorderBrush}&quot;/&gt;
&lt;Setter Property=&quot;BorderThickness&quot; Value=&quot;{ThemeResource TextControlBorderThemeThickness}&quot;/&gt;
&lt;Setter Property=&quot;FontFamily&quot; Value=&quot;{ThemeResource ContentControlThemeFontFamily}&quot;/&gt;
&lt;Setter Property=&quot;FontSize&quot; Value=&quot;{ThemeResource ControlContentThemeFontSize}&quot;/&gt;
&lt;Setter Property=&quot;ScrollViewer.HorizontalScrollMode&quot; Value=&quot;Auto&quot;/&gt;
&lt;Setter Property=&quot;ScrollViewer.VerticalScrollMode&quot; Value=&quot;Auto&quot;/&gt;
&lt;Setter Property=&quot;ScrollViewer.HorizontalScrollBarVisibility&quot; Value=&quot;Auto&quot;/&gt;
&lt;Setter Property=&quot;ScrollViewer.VerticalScrollBarVisibility&quot; Value=&quot;Auto&quot;/&gt;
&lt;Setter Property=&quot;ScrollViewer.IsDeferredScrollingEnabled&quot; Value=&quot;False&quot;/&gt;
&lt;Setter Property=&quot;TextWrapping&quot; Value=&quot;Wrap&quot;/&gt;
&lt;Setter Property=&quot;Padding&quot; Value=&quot;{ThemeResource TextControlThemePadding}&quot;/&gt;
&lt;Setter Property=&quot;UseSystemFocusVisuals&quot; Value=&quot;{ThemeResource IsApplicationFocusVisualKindReveal}&quot;/&gt;
&lt;Setter Property=&quot;ContextFlyout&quot; Value=&quot;{StaticResource TextControlCommandBarContextFlyout}&quot;/&gt;
&lt;Setter Property=&quot;SelectionFlyout&quot; Value=&quot;{StaticResource TextControlCommandBarSelectionFlyout}&quot;/&gt;
&lt;Setter Property=&quot;Template&quot;&gt;
&lt;Setter.Value&gt;
&lt;ControlTemplate TargetType=&quot;RichEditBox&quot;&gt;
&lt;Grid&gt;
&lt;Grid.RowDefinitions&gt;
&lt;RowDefinition Height=&quot;Auto&quot;/&gt;
&lt;RowDefinition Height=&quot;*&quot;/&gt;
&lt;RowDefinition Height=&quot;Auto&quot;/&gt;
&lt;/Grid.RowDefinitions&gt;
&lt;VisualStateManager.VisualStateGroups&gt;
&lt;VisualStateGroup x:Name=&quot;CommonStates&quot;&gt;
&lt;VisualState x:Name=&quot;Disabled&quot;&gt;
&lt;Storyboard&gt;
&lt;ObjectAnimationUsingKeyFrames Storyboard.TargetName=&quot;HeaderContentPresenter&quot; Storyboard.TargetProperty=&quot;Foreground&quot;&gt;
&lt;DiscreteObjectKeyFrame KeyTime=&quot;0&quot; Value=&quot;{ThemeResource TextControlHeaderForegroundDisabled}&quot;/&gt;
&lt;/ObjectAnimationUsingKeyFrames&gt;
&lt;ObjectAnimationUsingKeyFrames Storyboard.TargetName=&quot;BorderElement&quot; Storyboard.TargetProperty=&quot;Background&quot;&gt;
&lt;DiscreteObjectKeyFrame KeyTime=&quot;0&quot; Value=&quot;{ThemeResource TextControlBackgroundDisabled}&quot;/&gt;
&lt;/ObjectAnimationUsingKeyFrames&gt;
&lt;ObjectAnimationUsingKeyFrames Storyboard.TargetName=&quot;BorderElement&quot; Storyboard.TargetProperty=&quot;BorderBrush&quot;&gt;
&lt;DiscreteObjectKeyFrame KeyTime=&quot;0&quot; Value=&quot;{ThemeResource TextControlBorderBrushDisabled}&quot;/&gt;
&lt;/ObjectAnimationUsingKeyFrames&gt;
&lt;ObjectAnimationUsingKeyFrames Storyboard.TargetName=&quot;ContentElement&quot; Storyboard.TargetProperty=&quot;Foreground&quot;&gt;
&lt;DiscreteObjectKeyFrame KeyTime=&quot;0&quot; Value=&quot;{ThemeResource TextControlForegroundDisabled}&quot;/&gt;
&lt;/ObjectAnimationUsingKeyFrames&gt;
&lt;ObjectAnimationUsingKeyFrames Storyboard.TargetName=&quot;PlaceholderTextContentPresenter&quot; Storyboard.TargetProperty=&quot;Foreground&quot;&gt;
&lt;DiscreteObjectKeyFrame KeyTime=&quot;0&quot; Value=&quot;{ThemeResource TextControlPlaceholderForegroundDisabled}&quot;/&gt;
&lt;/ObjectAnimationUsingKeyFrames&gt;
&lt;/Storyboard&gt;
&lt;/VisualState&gt;
&lt;VisualState x:Name=&quot;Normal&quot;/&gt;
&lt;VisualState x:Name=&quot;PointerOver&quot;&gt;
&lt;Storyboard&gt;
&lt;ObjectAnimationUsingKeyFrames Storyboard.TargetName=&quot;BorderElement&quot; Storyboard.TargetProperty=&quot;BorderBrush&quot;&gt;
&lt;DiscreteObjectKeyFrame KeyTime=&quot;0&quot; Value=&quot;{ThemeResource TextControlBorderBrushPointerOver}&quot;/&gt;
&lt;/ObjectAnimationUsingKeyFrames&gt;
&lt;ObjectAnimationUsingKeyFrames Storyboard.TargetName=&quot;BorderElement&quot; Storyboard.TargetProperty=&quot;Background&quot;&gt;
&lt;DiscreteObjectKeyFrame KeyTime=&quot;0&quot; Value=&quot;{ThemeResource TextControlBackgroundPointerOver}&quot;/&gt;
&lt;/ObjectAnimationUsingKeyFrames&gt;
&lt;ObjectAnimationUsingKeyFrames Storyboard.TargetName=&quot;PlaceholderTextContentPresenter&quot; Storyboard.TargetProperty=&quot;Foreground&quot;&gt;
&lt;DiscreteObjectKeyFrame KeyTime=&quot;0&quot; Value=&quot;{ThemeResource TextControlPlaceholderForegroundPointerOver}&quot;/&gt;
&lt;/ObjectAnimationUsingKeyFrames&gt;
&lt;ObjectAnimationUsingKeyFrames Storyboard.TargetName=&quot;ContentElement&quot; Storyboard.TargetProperty=&quot;Foreground&quot;&gt;
&lt;DiscreteObjectKeyFrame KeyTime=&quot;0&quot; Value=&quot;{ThemeResource TextControlForegroundPointerOver}&quot;/&gt;
&lt;/ObjectAnimationUsingKeyFrames&gt;
&lt;/Storyboard&gt;
&lt;/VisualState&gt;
&lt;VisualState x:Name=&quot;Focused&quot;&gt;
&lt;Storyboard&gt;
&lt;ObjectAnimationUsingKeyFrames Storyboard.TargetName=&quot;PlaceholderTextContentPresenter&quot; Storyboard.TargetProperty=&quot;Foreground&quot;&gt;
&lt;DiscreteObjectKeyFrame KeyTime=&quot;0&quot; Value=&quot;{ThemeResource TextControlPlaceholderForegroundFocused}&quot;/&gt;
&lt;/ObjectAnimationUsingKeyFrames&gt;
&lt;ObjectAnimationUsingKeyFrames Storyboard.TargetName=&quot;BorderElement&quot; Storyboard.TargetProperty=&quot;Background&quot;&gt;
&lt;DiscreteObjectKeyFrame KeyTime=&quot;0&quot; Value=&quot;{ThemeResource TextControlBackgroundFocused}&quot;/&gt;
&lt;/ObjectAnimationUsingKeyFrames&gt;
&lt;ObjectAnimationUsingKeyFrames Storyboard.TargetName=&quot;BorderElement&quot; Storyboard.TargetProperty=&quot;BorderBrush&quot;&gt;
&lt;DiscreteObjectKeyFrame KeyTime=&quot;0&quot; Value=&quot;{ThemeResource TextControlBorderBrushFocused}&quot;/&gt;
&lt;/ObjectAnimationUsingKeyFrames&gt;
&lt;ObjectAnimationUsingKeyFrames Storyboard.TargetName=&quot;ContentElement&quot; Storyboard.TargetProperty=&quot;Foreground&quot;&gt;
&lt;DiscreteObjectKeyFrame KeyTime=&quot;0&quot; Value=&quot;{ThemeResource TextControlForegroundFocused}&quot;/&gt;
&lt;/ObjectAnimationUsingKeyFrames&gt;
&lt;ObjectAnimationUsingKeyFrames Storyboard.TargetName=&quot;ContentElement&quot; Storyboard.TargetProperty=&quot;RequestedTheme&quot;&gt;
&lt;DiscreteObjectKeyFrame KeyTime=&quot;0&quot; Value=&quot;Light&quot;/&gt;
&lt;/ObjectAnimationUsingKeyFrames&gt;
&lt;/Storyboard&gt;
&lt;/VisualState&gt;
&lt;/VisualStateGroup&gt;
&lt;/VisualStateManager.VisualStateGroups&gt;
&lt;ContentPresenter x:Name=&quot;HeaderContentPresenter&quot; ContentTemplate=&quot;{TemplateBinding HeaderTemplate}&quot; Content=&quot;{TemplateBinding Header}&quot; Foreground=&quot;{ThemeResource TextControlHeaderForeground}&quot; FontWeight=&quot;Normal&quot; Margin=&quot;{ThemeResource RichEditBoxTopHeaderMargin}&quot; Grid.Row=&quot;0&quot; TextWrapping=&quot;Wrap&quot; VerticalAlignment=&quot;Top&quot; Visibility=&quot;Collapsed&quot; x:DeferLoadStrategy=&quot;Lazy&quot;/&gt;
&lt;Border x:Name=&quot;BorderElement&quot; Background=&quot;{TemplateBinding Background}&quot; BorderBrush=&quot;{TemplateBinding BorderBrush}&quot; BorderThickness=&quot;{TemplateBinding BorderThickness}&quot; CornerRadius=&quot;{TemplateBinding CornerRadius}&quot; MinWidth=&quot;{ThemeResource TextControlThemeMinWidth}&quot; MinHeight=&quot;{ThemeResource TextControlThemeMinHeight}&quot; Grid.RowSpan=&quot;1&quot; Grid.Row=&quot;1&quot;/&gt;
&lt;ScrollViewer x:Name=&quot;ContentElement&quot;  VerticalAlignment=&quot;Center&quot; AutomationProperties.AccessibilityView=&quot;Raw&quot; HorizontalScrollBarVisibility=&quot;{TemplateBinding ScrollViewer.HorizontalScrollBarVisibility}&quot; HorizontalScrollMode=&quot;{TemplateBinding ScrollViewer.HorizontalScrollMode}&quot; IsDeferredScrollingEnabled=&quot;{TemplateBinding ScrollViewer.IsDeferredScrollingEnabled}&quot; IsTabStop=&quot;False&quot; IsVerticalRailEnabled=&quot;{TemplateBinding ScrollViewer.IsVerticalRailEnabled}&quot; IsHorizontalRailEnabled=&quot;{TemplateBinding ScrollViewer.IsHorizontalRailEnabled}&quot; Margin=&quot;{TemplateBinding BorderThickness}&quot; Padding=&quot;{TemplateBinding Padding}&quot; Grid.Row=&quot;1&quot; VerticalScrollBarVisibility=&quot;{TemplateBinding ScrollViewer.VerticalScrollBarVisibility}&quot; VerticalScrollMode=&quot;{TemplateBinding ScrollViewer.VerticalScrollMode}&quot; ZoomMode=&quot;Disabled&quot;/&gt;
&lt;TextBlock x:Name=&quot;PlaceholderTextContentPresenter&quot; Foreground=&quot;{ThemeResource TextControlPlaceholderForeground}&quot; IsHitTestVisible=&quot;False&quot; Margin=&quot;{TemplateBinding BorderThickness}&quot; Padding=&quot;{TemplateBinding Padding}&quot; Grid.Row=&quot;1&quot; TextAlignment=&quot;{TemplateBinding TextAlignment}&quot; TextWrapping=&quot;{TemplateBinding TextWrapping}&quot; Text=&quot;{TemplateBinding PlaceholderText}&quot;/&gt;
&lt;ContentPresenter x:Name=&quot;DescriptionPresenter&quot; AutomationProperties.AccessibilityView=&quot;Raw&quot; Content=&quot;{TemplateBinding Description}&quot; Foreground=&quot;{ThemeResource SystemControlDescriptionTextForegroundBrush}&quot; Grid.Row=&quot;2&quot; x:Load=&quot;False&quot;/&gt;
&lt;/Grid&gt;
&lt;/ControlTemplate&gt;
&lt;/Setter.Value&gt;
&lt;/Setter&gt;
&lt;/Style&gt;

The result looks like:

垂直文本对齐在UWP中的RichEditBox。

huangapple
  • 本文由 发表于 2023年4月17日 13:40:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76032007.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定