2
我有框架和页框内的窗口。如何将文本框脱字符位置设置为文本结尾?
页面有一个文本框,当有人开始写入该文本框时,我想将该框架导航到另一个页面(类似于谷歌搜索,当您开始编写时,搜索结果立即出现在不同视图中)。
我现在对这两个页面对象都使用相同的DataContext,因此它们都可以读取属性searledText的值,该属性我已绑定到每个页面上的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();
}
}
}
页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();
}
}
}
但哪里?请阅读SearchSepanded中FocusSearchBox方法的评论。执行方法时,searcCustomer属性的长度为0。 – Krab