同时使用 GotFocus 和 TextChanged - Windows Phone
Using GotFocus and TextChanged simultaneously - Windows Phone
我有一个 AutoSuggestBox
设置为同时处理事件 GotFocus & TextChanged。我已经清除了 GotFocus 事件中文本框中的文本。现在的问题是,当我 select AutoSuggestBox
中的任何建议时,在 selecting 之后它调用 GotFocus 事件处理程序并清除 select从中编辑文本。
这是使用 AutoSuggestBox 的 MainPage.xaml
代码:
<AutoSuggestBox
x:Name="auto_text_from"
HorizontalAlignment="Left"
VerticalAlignment="Center"
PlaceholderText="Enter Source"
Height="auto"
Width="280"
GotFocus="auto_text_from_GotFocus"
TextChanged="AutoSuggestBox_TextChanged"/>
这是我在 MainPage.xaml.cs
中编写的代码:
private void auto_text_from_GotFocus(object sender, RoutedEventArgs e)
{
auto_text_from.Text = "";
}
string[] PreviouslyDefinedStringArray = new string[] {"Alwar","Ajmer","Bharatpur","Bhilwara",
"Banswada","Jaipur","Jodhpur","Kota","Udaipur"};
private void AutoSuggestBox_TextChanged(AutoSuggestBox sender,AutoSuggestBoxTextChangedEventArgs args)
{
List<string> myList = new List<string>();
foreach (string myString in PreviouslyDefinedStringArray)
{
if (myString.ToLower().Contains(sender.Text.ToLower()) == true)
{
myList.Add(myString);
}
}
sender.ItemsSource = myList;
}
我想同时使用这两个事件处理程序。 GotFocus
用于清除文本框的数据,TextChanged
用于显示在其中写入文本的建议。
请建议我做同样的事情。
提前致谢:)
如果 AutoSuggestBox
有一个事件来处理建议词的选择,例如“SuggestionChosen
”,一个可能的解决方案是使用在各种处理程序之间管理的私有标志。
设置私有字段:
private bool _isSelectingSuggestion;
Link 像 OnSuggestionChosen
这样的方法处理程序到事件 SuggestionChosen
并像这样实现它:
private void OnSuggestionChosen(object sender, RoutedEventArgs e)
{
_isSelectingSuggestion = true;
}
然后,在 GotFocus 中,像这样检查标志:
private void auto_text_from_GotFocus(object sender, RoutedEventArgs e)
{
if (_isSelectingSuggestion)
e.Handled = true;
else
auto_text_from.Text = "";
_isSelectingSuggestion = false;
}
显然这仅在 GotFocus
之前引发 SuggestionChosen
时有效:当 GotFocus
开始时,它会像这样进行:"ok, I've got focus because a suggestion was chosen just a moment ago? If it's true, I must not clear my Text! Otherwise, I shall clear it!".
告诉我这对你有用!
@MK87:是的,稍作改动就可以了!! :)
private bool _isSelectingSuggestion;
private void OnSuggestionChosen(AutoSuggestBox sender, AutoSuggestBoxSuggestionChosenEventArgs args)
{
_isSelectingSuggestion = true;
}
private void auto_text_from_GotFocus(object sender, RoutedEventArgs e)
{
if (!_isSelectingSuggestion)
auto_text_from.Text = "";
_isSelectingSuggestion = false;
}
我不得不删除这一行:
e.Handled == true;
因为它给我的错误是 RoutedEventArgs does not contain a definition for 'Handled'
。
感谢您的帮助:) :)
我有一个 AutoSuggestBox
设置为同时处理事件 GotFocus & TextChanged。我已经清除了 GotFocus 事件中文本框中的文本。现在的问题是,当我 select AutoSuggestBox
中的任何建议时,在 selecting 之后它调用 GotFocus 事件处理程序并清除 select从中编辑文本。
这是使用 AutoSuggestBox 的 MainPage.xaml
代码:
<AutoSuggestBox
x:Name="auto_text_from"
HorizontalAlignment="Left"
VerticalAlignment="Center"
PlaceholderText="Enter Source"
Height="auto"
Width="280"
GotFocus="auto_text_from_GotFocus"
TextChanged="AutoSuggestBox_TextChanged"/>
这是我在 MainPage.xaml.cs
中编写的代码:
private void auto_text_from_GotFocus(object sender, RoutedEventArgs e)
{
auto_text_from.Text = "";
}
string[] PreviouslyDefinedStringArray = new string[] {"Alwar","Ajmer","Bharatpur","Bhilwara",
"Banswada","Jaipur","Jodhpur","Kota","Udaipur"};
private void AutoSuggestBox_TextChanged(AutoSuggestBox sender,AutoSuggestBoxTextChangedEventArgs args)
{
List<string> myList = new List<string>();
foreach (string myString in PreviouslyDefinedStringArray)
{
if (myString.ToLower().Contains(sender.Text.ToLower()) == true)
{
myList.Add(myString);
}
}
sender.ItemsSource = myList;
}
我想同时使用这两个事件处理程序。 GotFocus
用于清除文本框的数据,TextChanged
用于显示在其中写入文本的建议。
请建议我做同样的事情。
提前致谢:)
如果 AutoSuggestBox
有一个事件来处理建议词的选择,例如“SuggestionChosen
”,一个可能的解决方案是使用在各种处理程序之间管理的私有标志。
设置私有字段:
private bool _isSelectingSuggestion;
Link 像 OnSuggestionChosen
这样的方法处理程序到事件 SuggestionChosen
并像这样实现它:
private void OnSuggestionChosen(object sender, RoutedEventArgs e)
{
_isSelectingSuggestion = true;
}
然后,在 GotFocus 中,像这样检查标志:
private void auto_text_from_GotFocus(object sender, RoutedEventArgs e)
{
if (_isSelectingSuggestion)
e.Handled = true;
else
auto_text_from.Text = "";
_isSelectingSuggestion = false;
}
显然这仅在 GotFocus
之前引发 SuggestionChosen
时有效:当 GotFocus
开始时,它会像这样进行:"ok, I've got focus because a suggestion was chosen just a moment ago? If it's true, I must not clear my Text! Otherwise, I shall clear it!".
告诉我这对你有用!
@MK87:是的,稍作改动就可以了!! :)
private bool _isSelectingSuggestion;
private void OnSuggestionChosen(AutoSuggestBox sender, AutoSuggestBoxSuggestionChosenEventArgs args)
{
_isSelectingSuggestion = true;
}
private void auto_text_from_GotFocus(object sender, RoutedEventArgs e)
{
if (!_isSelectingSuggestion)
auto_text_from.Text = "";
_isSelectingSuggestion = false;
}
我不得不删除这一行:
e.Handled == true;
因为它给我的错误是 RoutedEventArgs does not contain a definition for 'Handled'
。
感谢您的帮助:) :)