Вопрос

I'm stuck on a strange problem. I have an AutoCompleteBox in my view

<sdk:AutoCompleteBox x:Name="txtSIA" 
 Grid.ColumnSpan="1" Grid.Row="1" Grid.Column="1"
 SelectedItem="{Binding SIA, Mode=TwoWay, ValidatesOnNotifyDataErrors=True}" 
 Text="{Binding TextSIA, Mode=TwoWay}"
 KeyUp="TxtSIA_KeyUp"
 Populating="SIANonSIU_Populating" 
 Style="{StaticResource AutoCompleteStyle}"  
/>

I implemented a field validator that check if its text isn't null or an empty string. It works pretty well but the tricky part is that I have a button that Reset all my controls values, which code from my viewmodel is:

void BtnReset_OnClick(RoutedEventArgs e)
{
    SIA = new SIA();
    TextSIA = string.Empty;
    BtnGeneralIsEnabled = false;
    DataGridSource = null;
}

Whenever I click it and then write in my AutoCompleteBox, the AutoCompleteBox is never empty or null even in my code behind in a key up event listener.

Here are some picture to illustrate my point:

Text = ab

Text = a

Text supposed to be =

Это было полезно?

Решение 2

I found an answer here

To fix that but we have to create a new autocompletebox and override the OnApplyTemplate method.

public class CustomAutoComplete : AutoCompleteBox
{
TextBox mytext;

public override void OnApplyTemplate()
{
    base.OnApplyTemplate();
    mytext = GetTemplateChild("Text") as TextBox;
    mytext.TextChanged += new System.Windows.Controls.TextChangedEventHandler(mytext_TextChanged);
}

void mytext_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
{
    this.Text = mytext.Text;
    OnTextChanged(new RoutedEventArgs());
}

}

Другие советы

Register to the TextChanged event of the AutoCompleteBox instead of the KeyUp:

"Occurs when the text in the text box portion of the AutoCompleteBox changes."

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top