如何将文本框插入符号位置设置为文本末尾

本文关键字:文本 设置 位置 插入 符号 | 更新日期: 2023-09-27 17:57:50

我有一个框架内有Frame和Page的Window。

该页面有一个TextBox,当有人开始向该TextBox写入时,我想将该框架导航到另一个页面(类似于谷歌搜索,当您开始写作时,搜索结果立即显示在不同的视图上)。

我现在对两个Page对象使用相同的DataContext,这样它们都可以读取属性searchedText的值,我已经将其绑定到每个Page上的TextBox。

页面搜索.xml:

<Page x:Class="Customer_UI.Search"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      mc:Ignorable="d" 
      d:DesignHeight="300" d:DesignWidth="300"
      Title="Search">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <TextBox Name="searchBox" Grid.Column="0" Grid.Row="0" Margin="30" FontSize="28" Padding="10" TextAlignment="Center" VerticalAlignment="Center" BorderThickness="5" KeyUp="TextBox_KeyUp" Text="{Binding Path=SearchedCustomer}" >
        </TextBox>
    </Grid>
</Page>

搜索代码隐藏:

namespace Customer_UI
{
    public partial class Search : Page
    {
        private void TextBox_KeyUp(object sender, KeyEventArgs e)
        {
            SearchExpanded searchExpanded = new SearchExpanded();
            searchExpanded.DataContext = this.DataContext;
            (this.DataContext as MainWindow.MainWindowContext).MainWindow.MainFrame.Navigate(searchExpanded);
            searchExpanded.FocusSearchBox();
        }
    }
}

页面搜索扩展.xml:

<Page x:Class="Customer_UI.SearchExpanded"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      mc:Ignorable="d" 
      d:DesignHeight="300" d:DesignWidth="300"
      Title="SearchExpanded">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <TextBox Name="searchBox" Grid.Column="0" Grid.Row="0" FontSize="18" Padding="10" TextAlignment="Center" VerticalAlignment="Center" BorderThickness="5" Text="{Binding Path=SearchedCustomer}" >
        </TextBox>
    </Grid>
</Page>

搜索扩展代码隐藏:

namespace Customer_UI
{
    public partial class SearchExpanded : Page
    {
        public void FocusSearchBox() 
        {   
            MainWindow.MainWindowContext dc = DataContext as MainWindow.MainWindowContext;
            // this has no output, the input from TextBox on Page that causes navigation to this page is probably still not reflected to dataContext.searchedCustomer property
            Console.WriteLine(dc.SearchedCustomer);
            // problem is that this time dc.SearchedCustomer has still lenght zero
            searchBox.Focus();
        }
    }
}

如何将文本框插入符号位置设置为文本末尾

将SelectionStart属性设置为文本的长度。