This is a migrated thread and some comments may be shown as answers.

SelectedItem bound to a property thats never null

7 Answers 160 Views
AutoCompleteBox
This is a migrated thread and some comments may be shown as answers.
Peter
Top achievements
Rank 1
Peter asked on 08 Nov 2012, 02:03 AM
Hi,

For many properties I often initialise the underlying field in the get accessor. ie
public User SelectedCreatedBy
        {
            get
            {
                if (_SelectedCreatedBy != null)
                    _SelectedCreatedBy = new User();
                return _SelectedCreatedBy;
            }
            set
            {
                if (_SelectedCreatedBy != value)
                {
                    _SelectedCreatedBy = value;
                    RaisePropertyChanged(() => SelectedCreatedBy);
                }
            }
        }
But binding "SelectedItem" to this property crashes the program when I start typing. If I remove the initialisation part, it works. ie:
public User SelectedCreatedBy
        {
            get
            {
                return _SelectedCreatedBy;
            }
            set
            {
                if (_SelectedCreatedBy != value)
                {
                    _SelectedCreatedBy = value;
                    RaisePropertyChanged(() => SelectedCreatedBy);
                }
            }
        }

Was this a design decision with RadAutoCompleteBox or a bug? I've never had this problem with any other telerik controls.

7 Answers, 1 is accepted

Sort by
0
Ivo
Telerik team
answered on 12 Nov 2012, 12:19 PM
Hi Peter,

Thanks for your feedback.

I just tested this into a sample project, but I was not able to reproduce it. It would be great if you open a new support ticket and send us a sample project so we will be able to investigate this further.

Greetings,
Ivo
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Ivo
Telerik team
answered on 12 Nov 2012, 01:53 PM
Hello Peter,

I have reproduced this at our side. It seems lika an issue and we will do our best to fix it for the next internal build.
Have in mind that writing text into the RadAutoCompleteBox clears the selection when using single selection mode. This means that if you use TwoWay binding every time you write into the control the default value will be set into your ViewModel and this will make the control not really usable. I think that the only way to get this working as expected would be to use OneWayToSource binding.

Greetings,
Ivo
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Peter
Top achievements
Rank 1
answered on 13 Nov 2012, 02:27 AM
From what I've tested, the ViewModel property will only be updated when the selection is made, ie not just typing. This is what I want.
There are times when I want to set the property in the VM and see it updated in the UI, thus setting it OneWayToSource would not be suitable.

Are you making a PITS entry I can subscribe to? I created a project for it (prior to reading that you reproduced the error). If it'll help I can attach it.
0
Ivo
Telerik team
answered on 16 Nov 2012, 03:43 PM
Hi Peter,

The issue you reported is now fixed and the fix will be included into the next internal build.

Thanks for your feedback once again.

All the best,
Ivo
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Peter
Top achievements
Rank 1
answered on 03 Dec 2012, 12:10 AM
Hi,

I've got the latest version 1129.40, added the below code, and my program no longer crashes, but neither does it show or do anything when I type a letter. Removing the below code again, and the control works as expected.
if (_SelectedCreatedBy != null)
     _SelectedCreatedBy = new User();
Was the error fixed or simply caught? :) As I mentioned previously, I use this initialise for all the telerik controls, so this AutoComplete control appears to operate differently.
0
Ivo
Telerik team
answered on 04 Dec 2012, 08:48 AM
Hi Peter,

The error you reported is fixed and not simply caught. However as I tried to explain you into my previous replies this approach will not work as you expect. Changing the SearchText of the RadAutoCompleteBox (this happens on every key stroke) clears the selection of the control. Clearing the selection will go through the getter of the SelectedCreatedBy property and will set it to a new User. Finally the control will be notified that the SelectedCreatedBy changed its value and it will update its SelectedItem value. Updating the SelectedItem will change the RadAutoCompleteBox' SearchText to the string representation of the SelectedItem - into your case this is an empty string. That's why when writing a single letter into the control nothing changes.

Greetings,
Ivo
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Peter
Top achievements
Rank 1
answered on 06 Dec 2012, 01:30 AM
Ok, thanks for explaining it clearer Ivo. That explains why the text doesn't display, and does it also prevent the drop-down contents from showing?

Can you see the way this works changing down the track? (I'm trialling this control after using the AutoComplete control from the wpf toolkit which can operate using my method.)
Tags
AutoCompleteBox
Asked by
Peter
Top achievements
Rank 1
Answers by
Ivo
Telerik team
Peter
Top achievements
Rank 1
Share this question
or