同时使用GotFocus和TextChanged - Windows Phone [英] Using GotFocus and TextChanged simultaneously - Windows Phone
问题描述
我有一个 AutoSuggestBox
,它设置为处理事件 GotFocus & TextChanged 同时。我已经从 GotFocus 事件中的文本框中清除了文本。现在的问题是,当我选择 AutoSuggestBox
中的任何建议时,选择它后调用 GotFocus 事件处理程序,并从中清除所选文本。
这是使用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 斋浦尔, 焦特布尔, 哥打, 乌代布尔};
private void AutoSuggestBox_TextChanged(AutoSuggestBox sender,AutoSuggestBoxTextChangedEventArgs args)
{
列表< string> myList = new List< string>();
foreach(string myString in PreviouslyDefinedStringArray)
{
if(myString.ToLower()。包含(sender.Text.ToLower())== true)
{
myList.Add(myString);
}
}
sender.ItemsSource = myList;
}
我想使用这两个事件处理程序。 GotFocus
用于清除文本框的数据,以及 TextChanged
,用于显示在其中写入文本的建议。 p>
请以任何方式提供相同的建议。
感谢提前:)
如果 AutoSuggestBox
有一个事件来处理建议的单词的选择,如 SuggestionChosen
,一个可能的解决方案是使用在各种处理程序之间管理的私有标志。
设置一个私有字段:
private bool _isSelectingSuggestion;链接一个方法处理程序,如 OnSuggestionChosen
,将$ / $ / code $
事件 SuggestionChosen
并实现它:
private void OnSuggestionChosen对象发件人,RoutedEventArgs e)
{
_isSelectingSuggestion = true;
}
然后,在GotFocus中,检查这样的标志:
private void auto_text_from_GotFocus(object sender,RoutedEventArgs e)
{
if(_isSelectingSuggestion)
e.Handled =真正;
else
auto_text_from.Text =;
_isSelectingSuggestion = false;
}
显然这只能在 SuggestionChosen
在 GotFocus
之前被提升:当 GotFocus
开始时,它就像:好吧,我有焦点因为刚刚选择了一个建议,如果是真的,我不能清除我的文本!否则我会清除它!。
让我知道这个为您工作!
I have an AutoSuggestBox
which is set to handle the events GotFocus & TextChanged simultaneously. I have cleared the text from the text box in GotFocus event. Now the problem is that when I select any of the suggestions in AutoSuggestBox
, after selecting it calls the GotFocus event handler and clears the selected text from it.
This is the MainPage.xaml
code using the AutoSuggestBox:
<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"/>
And this one is the code which I have written in 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;
}
I want to use both of the event handlers. GotFocus
for clearing the data of Text box, and TextChanged
for showing the suggestions of writing the text in it.
Please suggest me any way to do the same.
Thanks in Advance :)
If AutoSuggestBox
has an event to handle the selection of a suggested word, like "SuggestionChosen
", a possibile solution is using a private flag managed between the various handlers.
Set a private field:
private bool _isSelectingSuggestion;
Link a method-handler like OnSuggestionChosen
to the event SuggestionChosen
and implement it like that:
private void OnSuggestionChosen(object sender, RoutedEventArgs e)
{
_isSelectingSuggestion = true;
}
Then, in GotFocus, check the flag like this:
private void auto_text_from_GotFocus(object sender, RoutedEventArgs e)
{
if (_isSelectingSuggestion)
e.Handled = true;
else
auto_text_from.Text = "";
_isSelectingSuggestion = false;
}
Obviously this works only if SuggestionChosen
is raised before GotFocus
: when GotFocus
begins, it proceeds like: "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!".
Let me know it this work for you!
这篇关于同时使用GotFocus和TextChanged - Windows Phone的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!