如何设置textbox插入位置到文本结尾?

时间:2014-04-10 02:57:01

标签: c# .net wpf xaml

我在框架内有框架和页面的窗口。

该页面有一个TextBox,当有人开始写入该TextBox时,我希望将该帧导航到另一个页面(类似于Google搜索,当您开始编写时,搜索结果会立即出现在不同的视图中)。

我现在对两个Page对象使用相同的DataContext,因此他们都可以读取属性searchText的值,我已经绑定到每个页面上的TextBox。

页面Search.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();
        }
    }
}

Page SearchExpanded.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>

SearchExpanded代码背后:

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();
        }
    }
}

1 个答案:

答案 0 :(得分:1)

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