SelectedItem set to nothing in OnApplyTemplate()

Mar 24, 2015 at 9:42 AM
Hello,

here again for another issue, or maybe just a behaviour that does not fit my needs.

I have a data loading phase during which I fill all the input controls on my GUI, including the AutoCompleteTextbox. During this phase I encountered my previous issue

Well, testing the correct data checking in order to enable the "Save" button in my Gui, I discovered that the check fails because the AutoCompleteTextBox.SelectedItem was back to Nothing value.

Following the debug trace I noticed that in the OnApplyTemplate() there is the following part of code:
If SelectedItem IsNot Nothing Then                    
         Editor.Text = BindingEvaluator.Evaluate(SelectedItem)
End If
and, after the instruction inside the control block, the SelectedItem turns to Nothing value.
Going a little deeper, I found a SetSelectedItem(Nothing) inside the OnEditorTextChanged handler so I commented it.

Anyway, once the data loading is completed, it seems that AutoCompleteTextBox.SelectedItem turns Nothing again and I was not able to figure out why.

My need is to have the item selected always in place in order to manage both text to display and code to use for unique item identification.

Do you have any idea?

Cheers
Giacomo
Coordinator
Mar 24, 2015 at 10:21 AM
Hi Giacomo,

Can you please try moving
if (SelectedItem != null)
{
    Editor.Text = BindingEvaluator.Evaluate(SelectedItem);
}
before we add TextChanged event handler to Editor
Editor.TextChanged += OnEditorTextChanged;
Mar 24, 2015 at 11:04 AM
Hi,

I tried, and actually the SelectedItem is not Nothing after TextChanged is fired
But, for some reason, it is Nothing when the checking inside Command.CanExecute binded to the save data button are performed

Cheers
Giacomo
Coordinator
Mar 24, 2015 at 11:12 AM

Can you please check if CanExecute method is executing before you initialize the SelectedItem property. I guess this is the case.

Mar 24, 2015 at 11:38 AM
Hi,

CanExecute is the check handler of the Command binded to the button and it is executed continuosly by the WPFramework when the application is idle to set the button enabled or disabled.
I guess at some point it should be executed when the SelectedItem property is initialized.
Or am I missing something?

Cheers
Giacomo
Coordinator
Mar 24, 2015 at 11:48 AM
If you are using DelegateCommand, try DelegateCommand.RaiseCanExecuteChanged method on the setter of property which is bound to SelectedItem property. You must raise CanExecuteChanged event whenever the value of any property changes, which takes part in validation.


Cheers!
Deepak
Mar 24, 2015 at 1:33 PM
EDIT:

I just got that moving the piece of code
if (SelectedItem != null)
{
    Editor.Text = BindingEvaluator.Evaluate(SelectedItem);
}
before add the TextChanged event handler to Editor leads the AutoCompleteTextBox to not display the text inside itself.

I also by-passed the CanExecute event handler setting the return value always to True and then performe the data checking when the user click the Save button, finding that the AutoCompleteTextBox.SelectedItem is Nothing while I set it to the proper object during the data loading

Cheers
Giacomo
Coordinator
Mar 25, 2015 at 2:48 AM
Can you send me a sample app with the behavior.

Cheers!
Deepak
Mar 25, 2015 at 8:30 AM
Hi,

well, it's not so easy because I use AutoCompleteTextBox with a provider binded to a Linq Entity with a model that maps a MySQL database.

Anyway, I was trying to go deeper in the data loading process and the behaviour of the item selection on AutoCompleteTextBox.
Here are the steps:

1) During the data loading procedure I set actbPortoPartenza.SelectedItem = portoPartenza where "actbPortoPartenza" is the AutoCompleteTextBox and "portoPartenza" is a Linq Entity with Port Code and Port Description as main properties

2) after setting the value for the actbPortoPartenza.SelectedItem the OnSelectedItemChanged event is raised with the e.OldValue = Nothing and e.NewValue set to the correct Linq Entity Object, as expected. Anyway, at that moment the AutoCompleteTextBox.Editor is nothing so no action is performed

3) then, the OnApplyTemplate event is raised (here I have the Editor.Text = BindingEvaluator.Evaluate(SelectedItem) before adding the TextChanged event handler to the Editor). At the end of this call, I have the right SelectedItem and Text set in the AutoCompleteTextBox

4) after that the templates of all other controls in the GUI are applied from first to last.

5) Then it is raised the AutoCompleteTextBox OnSelectedItemChanged and on this handling we meet the issue because while the e.OldValue is the correct Linq Entity, the e.NewValue is Nothing. So the call act.Editor.Text = act.BindingEvaluator.Evaluate(e.NewValue) leads to a condition in which Editor.Text is set to empty string and the SelectedItem is set to Nothing

I still can't get why the AutoCompleteTextBox OnSelectedItemChanged is raised at that point of code and why its e.NewValue is Nothing but maybe you can have a better idea on this.

Cheers
Giacomo
Dec 8, 2015 at 6:55 AM
Hi.
For me also was helpful to move
if (SelectedItem != null)
{
    Editor.Text = BindingEvaluator.Evaluate(SelectedItem);
}
before TextChanged event.

For me it happens when I set selectedItem for invisible control (AutoCompleteBox was on second TabItem) and everything was fine until I don't go to that TabItem. After I click on TabItem (to make it visible), SelectedItem goes to null.