如果用户点击我的RichEditBox
,则插入指针应出现在行的开头,而不是字符串末尾。例如,假设RichEditBox
可以为其Height
包含5行,因此当用户点击第3行时,插入指针应该位于第3行的开头而不是第1行。我很难解释,希望你能理解。
为实现这一点,我想到默认情况下将换行设置为RichEditBox
的结尾。
解决方案:
我使用下面的代码来做,但它没有工作
var oldActualHeight = PATH_RICH_EDIT_BOX.ActualHeight;
while (PATH_RICH_EDIT_BOX.ActualHeight <= oldActualHeight)
{
PATH_RICH_EDIT_BOX.Document.GetText(Windows.UI.Text.TextGetOptions.None, out string a);
PATH_RICH_EDIT_BOX.Document.SetText(Windows.UI.Text.TextSetOptions.None, a + Environment.NewLine);
}
我在XAML中的RichEditBox代码
<Page.Resources>
<Style x:Key="RichEditBoxStyle" TargetType="RichEditBox">
<Setter Property="MinWidth" Value="{ThemeResource TextControlThemeMinWidth}"/>
<Setter Property="MinHeight" Value="{ThemeResource TextControlThemeMinHeight}"/>
<Setter Property="Foreground" Value="{ThemeResource TextControlForeground}"/>
<Setter Property="Background" Value="{ThemeResource TextControlBackground}"/>
<Setter Property="SelectionHighlightColor" Value="{ThemeResource TextControlSelectionHighlightColor}"/>
<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="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>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<ContentPresenter x:Name="HeaderContentPresenter" Grid.ColumnSpan="2" ContentTemplate="{TemplateBinding HeaderTemplate}" Content="{TemplateBinding Header}" Foreground="{ThemeResource TextControlHeaderForeground}" FontWeight="Normal" Margin="0,0,0,8" Grid.Row="0" Visibility="Collapsed" x:DeferLoadStrategy="Lazy"/>
<ScrollViewer x:Name="ContentElement" 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" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
<ContentControl x:Name="PlaceholderTextContentPresenter" Grid.ColumnSpan="2" Content="{TemplateBinding PlaceholderText}" Foreground="{ThemeResource TextControlPlaceholderForeground}" IsHitTestVisible="False" IsTabStop="False" Margin="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}" Grid.Row="1"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Page.Resources>
<Grid Name="MainGrid" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal">
<InkToolbar TargetInkCanvas="{x:Bind PATH_INK_CANVAS}"/>
<Button Name="ChangeButton" Content="Change" Click="ChangeButton_Click"/>
</StackPanel>
<ScrollViewer Grid.Row="1" HorizontalScrollBarVisibility="Visible" Visibility="Visible">
<Grid>
<InkCanvas Name="PATH_INK_CANVAS" Canvas.ZIndex="-1"/>
<RichEditBox Name="PATH_RICH_EDIT_BOX" PlaceholderText="Input Text" Style="{StaticResource RichEditBoxStyle}"/>
</Grid>
</ScrollViewer>
</Grid>
答案 0 :(得分:0)
目前,没有这样的api可以获得RichEditBox
的每一行。因此,如果您想将光标移动到行首。你可以得到线的起始位置并将其聚焦。
最重要的是,你应该得到RichEditBox
文件的起点。
public MainPage()
{
this.InitializeComponent();
ITextSelection selection = PATH_RICH_EDIT_BOX.Document.Selection;
selection.StartPosition = 0;
selection.EndPosition = 0;
originPoint = new Point();
selection.GetPoint(HorizontalCharacterAlignment.Left, VerticalCharacterAlignment.Baseline, PointOptions.Start, out originPoint);
}
关于每个线起点的点,它们具有相同的X
。因此,您可以通过当前光标位置获取行起点。
private void ChangeButton_Click(object sender, RoutedEventArgs e)
{
Point point = new Point();
ITextSelection selection = PATH_RICH_EDIT_BOX.Document.Selection;
var sta = selection.StartPosition;
var en = selection.EndPosition;
selection.GetPoint(HorizontalCharacterAlignment.Left, VerticalCharacterAlignment.Baseline, PointOptions.Start, out point);
point = new Point(originPoint.X, point.Y);
var pgformat = selection.ParagraphFormat;
selection.SetPoint(point, PointOptions.Start, false);
var start = selection.StartPosition;
var end = selection.EndPosition;
PATH_RICH_EDIT_BOX.Document.Selection.SetRange(start, end + 1);
PATH_RICH_EDIT_BOX.Focus(FocusState.Pointer);
}
请注意,上一行的一个空格的结束位置与当前行的起始位置相同。所以你应该使用一个长度范围来区分它们。
PATH_RICH_EDIT_BOX.Document.Selection.SetRange(start, end + 1);