【问题标题】:Padding the content of a RichEditBox in a C# WinRT app在 C# WinRT 应用程序中填充 RichEditBox 的内容
【发布时间】:2014-07-26 05:49:11
【问题描述】:

我正在尝试在 XAML/C# WinRT 应用程序中将填充应用到 RichEditBox 的顶部和底部,但它没有产生预期的效果。

基本上,当我将填充设置到这些边缘时,即使滚动控件的内容,填充也会产生空白空间。但是,我希望它填充 RichEditBox 的 content,这样当内容从顶部和底部边缘滚动时就没有可见的空白空间。

我该怎么做?

【问题讨论】:

    标签: c# xaml windows-runtime windows-store-apps padding


    【解决方案1】:

    您正在寻找的是正在绘制的内容中的Margin,因为您希望所绘制的内容实际放大,而不是距离边缘一定距离的内容。

    要添加此Margin,您需要编辑在样式中设置的模板。 The default style and template for the rich edit box is this

    <!-- Default style for System.Windows.Controls.RichEditBox -->
    <Style TargetType="RichEditBox">
        <Setter Property="MinWidth" Value="{ThemeResource TextControlThemeMinWidth}" />
        <Setter Property="MinHeight" Value="{ThemeResource TextControlThemeMinHeight}" />
        <Setter Property="Foreground" Value="{ThemeResource TextBoxForegroundThemeBrush}" />
        <Setter Property="Background" Value="{ThemeResource TextBoxBackgroundThemeBrush}" />
        <Setter Property="SelectionHighlightColor" Value="{ThemeResource TextSelectionHighlightColorThemeBrush}" />
        <Setter Property="BorderBrush" Value="{ThemeResource TextBoxBorderThemeBrush}" />
        <Setter Property="BorderThickness" Value="{ThemeResource TextControlBorderThemeThickness}" />
        <Setter Property="FontFamily" Value="{ThemeResource ContentControlThemeFontFamily}" />
        <Setter Property="FontSize" Value="{ThemeResource ControlContentThemeFontSize}" />
        <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto" />
        <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto" />
        <Setter Property="ScrollViewer.IsDeferredScrollingEnabled" Value="False" />
        <Setter Property="TextWrapping" Value="Wrap" />
        <Setter Property="Padding" Value="{ThemeResource TextControlThemePadding}"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="RichEditBox">
                    <Grid>
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Disabled">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="BackgroundElement"
                                                                       Storyboard.TargetProperty="Background">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource TextBoxDisabledBackgroundThemeBrush}" />
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="BorderElement"
                                                                       Storyboard.TargetProperty="BorderBrush">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource TextBoxDisabledBorderThemeBrush}" />
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentElement"
                                                                       Storyboard.TargetProperty="Foreground">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource TextBoxDisabledForegroundThemeBrush}" />
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="PlaceholderTextContentPresenter"
                                                                       Storyboard.TargetProperty="Foreground">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource TextBoxDisabledForegroundThemeBrush}" />
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Normal">
                                    <Storyboard>
                                        <DoubleAnimation Storyboard.TargetName="BackgroundElement"
                                                         Storyboard.TargetProperty="Opacity"
                                                         Duration="0"
                                                         To="{ThemeResource TextControlBackgroundThemeOpacity}" />
                                        <DoubleAnimation Storyboard.TargetName="BorderElement"
                                                         Storyboard.TargetProperty="Opacity"
                                                         Duration="0"
                                                         To="{ThemeResource TextControlBorderThemeOpacity}" />
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="PointerOver">
                                    <Storyboard>
                                        <DoubleAnimation Storyboard.TargetName="BackgroundElement"
                                                         Storyboard.TargetProperty="Opacity"
                                                         Duration="0"
                                                         To="{ThemeResource TextControlPointerOverBackgroundThemeOpacity}" />
                                        <DoubleAnimation Storyboard.TargetName="BorderElement"
                                                         Storyboard.TargetProperty="Opacity"
                                                         Duration="0"
                                                         To="{ThemeResource TextControlPointerOverBorderThemeOpacity}" />
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Focused" />
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="*"/>
                        </Grid.RowDefinitions>
                        <Border x:Name="BackgroundElement"
                                Grid.Row="1"
                                Background="{TemplateBinding Background}"
                                Margin="{TemplateBinding BorderThickness}"
                                Grid.ColumnSpan="2"
                                Grid.RowSpan="2"/>
                        <Border x:Name="BorderElement"
                                Grid.Row="1"
                                BorderBrush="{TemplateBinding BorderBrush}"
                                BorderThickness="{TemplateBinding BorderThickness}"
                                Grid.ColumnSpan="2"
                                Grid.RowSpan="1"/>
                        <ContentPresenter x:Name="HeaderContentPresenter"
                                          Grid.Row="0"
                                          Foreground="{ThemeResource TextBoxForegroundHeaderThemeBrush}"
                                          Margin="0,4,0,4"
                                          Grid.ColumnSpan="2"
                                          Content="{TemplateBinding Header}"
                                          ContentTemplate="{TemplateBinding HeaderTemplate}"
                                          FontWeight="Semilight" />
                        <ScrollViewer x:Name="ContentElement"
                                      Grid.Row="1"
                                      HorizontalScrollMode="{TemplateBinding ScrollViewer.HorizontalScrollMode}"
                                      HorizontalScrollBarVisibility="{TemplateBinding ScrollViewer.HorizontalScrollBarVisibility}"
                                      VerticalScrollMode="{TemplateBinding ScrollViewer.VerticalScrollMode}"
                                      VerticalScrollBarVisibility="{TemplateBinding ScrollViewer.VerticalScrollBarVisibility}"
                                      IsHorizontalRailEnabled="{TemplateBinding ScrollViewer.IsHorizontalRailEnabled}"
                                      IsVerticalRailEnabled="{TemplateBinding ScrollViewer.IsVerticalRailEnabled}"
                                      IsDeferredScrollingEnabled="{TemplateBinding ScrollViewer.IsDeferredScrollingEnabled}"
                                      Margin="{TemplateBinding BorderThickness}"
                                      Padding="{TemplateBinding Padding}"
                                      IsTabStop="False"
                                      ZoomMode="Disabled" 
                                      AutomationProperties.AccessibilityView="Raw"/>
                        <ContentControl x:Name="PlaceholderTextContentPresenter"
                                      Grid.Row="1"
                                      Foreground="{ThemeResource TextBoxPlaceholderTextThemeBrush}"
                                      Margin="{TemplateBinding BorderThickness}"
                                      Padding="{TemplateBinding Padding}"
                                      IsTabStop="False"
                                      Grid.ColumnSpan="2"
                                      Content="{TemplateBinding PlaceholderText}" 
                                      IsHitTestVisible="False"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    

    注意ScrollViewer 直接应用Padding。不幸的是,这不是你想要的。如果您将其替换为 ContentPresenter,将 Padding 作为 TemplateBinding 应用到其 Margin,它应该按照您的意愿呈现。

    <ScrollViewer 
      Grid.Row="1"
      HorizontalScrollMode="{TemplateBinding ScrollViewer.HorizontalScrollMode}"
      HorizontalScrollBarVisibility="{TemplateBinding ScrollViewer.HorizontalScrollBarVisibility}"
      VerticalScrollMode="{TemplateBinding ScrollViewer.VerticalScrollMode}"
      VerticalScrollBarVisibility="{TemplateBinding ScrollViewer.VerticalScrollBarVisibility}"
      IsHorizontalRailEnabled="{TemplateBinding ScrollViewer.IsHorizontalRailEnabled}"
      IsVerticalRailEnabled="{TemplateBinding ScrollViewer.IsVerticalRailEnabled}"
      IsDeferredScrollingEnabled="{TemplateBinding ScrollViewer.IsDeferredScrollingEnabled}"
      Margin="{TemplateBinding BorderThickness}"
      IsTabStop="False"
      ZoomMode="Disabled" 
      AutomationProperties.AccessibilityView="Raw">
          <ContentPresenter x:Name="ContentElement" Margin="{TemplateBinding Padding}"/>
    </ScrollViewer>
    

    请注意,您必须将ContentPresenterName 设置为ContentElement,以便控制逻辑知道它。

    希望这对编码有所帮助!

    【讨论】:

    • 我已将此标记为正确,因为它会产生所需的效果,但由于某种原因,我无法解决它似乎比我使用未经编辑的模板时运行得慢得多,尤其是在大文档,并且编辑文档变得非常滞后。这只发生在x:Name="ContentElement"ScrollViewer 移动到ContentPresenter 时。有什么办法可以解决这个问题吗?
    • 我真的不确定。您可能想尝试通过性能诊断来运行它,看看它是否提供了关于它需要这么长时间的提示。你在哪里声明风格?在页面本身中还是在ResourceDictionary 中合并到您的App.xaml 中?如果是前者,请尝试后者,反之亦然。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-12
    • 2012-05-29
    相关资源
    最近更新 更多