gpt4 book ai didi

c# - 如何将文本框插入符号位置设置为文本结尾?

转载 作者:太空宇宙 更新时间:2023-11-03 13:24:14 25 4
gpt4 key购买 nike

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

该页面有一个文本框,当有人开始向该文本框写入内容时,我想将该框架导航到另一个页面(类似于谷歌搜索,当您开始写入时搜索结果立即出现在不同的 View 中)。

我现在对两个 Page 对象使用相同的 DataContext,因此它们都可以读取我已绑定(bind)到每个 Page 上的 TextBox 的属性 searchedText 的值。

页面搜索.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>

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

最佳答案

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

关于c# - 如何将文本框插入符号位置设置为文本结尾?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22977842/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com