SelectedItem bound to a property thats never null

8 posts, 0 answers
  1. Peter
    Peter avatar
    95 posts
    Member since:
    Mar 2009

    Posted 07 Nov 2012 Link to this post

    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.
  2. Ivo
    Admin
    Ivo avatar
    390 posts

    Posted 12 Nov 2012 Link to this post

    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.

  3. UI for WPF is Visual Studio 2017 Ready
  4. Ivo
    Admin
    Ivo avatar
    390 posts

    Posted 12 Nov 2012 Link to this post

    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.

  5. Peter
    Peter avatar
    95 posts
    Member since:
    Mar 2009

    Posted 12 Nov 2012 Link to this post

    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.
  6. Ivo
    Admin
    Ivo avatar
    390 posts

    Posted 16 Nov 2012 Link to this post

    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.

  7. Peter
    Peter avatar
    95 posts
    Member since:
    Mar 2009

    Posted 02 Dec 2012 Link to this post

    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.
  8. Ivo
    Admin
    Ivo avatar
    390 posts

    Posted 04 Dec 2012 Link to this post

    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.

  9. Peter
    Peter avatar
    95 posts
    Member since:
    Mar 2009

    Posted 05 Dec 2012 Link to this post

    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.)
Back to Top
UI for WPF is Visual Studio 2017 Ready