Losing focus clears Text when IsFilteringEnabled="True"

6 posts, 1 answers
  1. Peter
    Peter avatar
    5 posts
    Member since:
    Jun 2014

    Posted 24 Feb 2015 Link to this post

    Hello,

    I want to use the Combobox with IsFilteringEnabled="True" to allow users to select items from a combobox.
    However users should also be able to enter new things, that are not (yet) available in the list. Kind of like a recently used list.

    When I enable filtering the combobox always clears itself when the focus is lost, thus my users can never enter anything new.
    Is there a way to turn this off?
    Or is there any other way I can do this?

    Thanks!

    Regards,
    Peter
  2. Nasko
    Admin
    Nasko avatar
    585 posts

    Posted 26 Feb 2015 Link to this post

    Hi Peter,

    By design when you type a text in an editable RadComboBox and no item matches this text as soon as the control loses focus it is set to null - that behavior is observed when IsFilteringEnabled is set either to True or False.  Could you please provide us some more detailed information about your scenario and the desired functionality -  thus, we could be able to provide you with a prompt approach how to achieve it? 

    We're looking forward to hearing from you.

    Regards,
    Nasko
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  3. UI for WPF is Visual Studio 2017 Ready
  4. Peter
    Peter avatar
    5 posts
    Member since:
    Jun 2014

    Posted 26 Feb 2015 in reply to Nasko Link to this post

    Hi Nasko,

    I know that the RadComboBox does this by design. Thats is why I'm asking how to achieve this.

    So here is what I'm trying to do:

    I want to be able to give the user a list of his recent queries, think database queries, and the combobox should allow to search in these recent queries, that is the reason for IsFilteringEnabled set to True.

    But the user has to be able to enter new queries that do not yet exist in the recent queries.
    And that is not possible when IsFilteringEnabled set to True because it clears the text when the focus is lost and the entered text is not in the recent queries list.

    I can't attach a project here so here is some snippets of code.

    The Xaml looks like this:
    <StackPanel>
            <telerik:RadComboBox Text="{Binding QueryText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                         ItemsSource="{Binding RecentQueries}"
                         IsEditable="True"
                         IsFilteringEnabled="True"
                         TextSearchMode="Contains"
                         OpenDropDownOnFocus="True"/>
             
            <Button Click="ExecuteQuery">
                Execute Query
            </Button>
        </StackPanel>

    And the code-behind
    public partial class MainWindow : Window, INotifyPropertyChanged
        {
            private string _queryText;
            private readonly ObservableCollection<string> _recentQueries = new ObservableCollection<string>(new []
            {
                "Some old query",
                "Some older query",
                "Some very old query",
            });
     
            public MainWindow()
            {
                InitializeComponent();
     
                DataContext = this;
            }
     
            public string QueryText
            {
                get { return _queryText; }
                set
                {
                    if(_queryText == value)
                        return;
                    _queryText = value;
     
                    OnPropertyChanged();
                }
            }
     
            public ObservableCollection<string> RecentQueries
            {
                get { return _recentQueries; }
            }
     
            private void ExecuteQuery(object sender, RoutedEventArgs e)
            {
                if(String.IsNullOrEmpty(QueryText))
                    return;
     
                //Execute the query here.
     
                //Add the query to the recentqueries list to make it available in the future
                if (!RecentQueries.Contains(QueryText))
                {
                    RecentQueries.Insert(0, QueryText);
                }
                else
                {
                    RecentQueries.Move(RecentQueries.IndexOf(QueryText), 0);
                }
            }
     
            public event PropertyChangedEventHandler PropertyChanged;
     
            [NotifyPropertyChangedInvocator]
            protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
            {
                var handler = PropertyChanged;
                if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }


    .
























  5. Answer
    Nasko
    Admin
    Nasko avatar
    585 posts

    Posted 26 Feb 2015 Link to this post

    Hello Peter,

    One possible approach that we could suggest you in order to achieve the desired functionality is to preserve the Text into a temporary variable and to handle the LostFocus event of RadComboBox. So, when the control loses its focus the event will fire and you could add the preserved variable into he collection of items. Please, check the attached sample project that demonstrates the described above approach.

    Hopes this helps.

    Regards,
    Nasko
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  6. Peter
    Peter avatar
    5 posts
    Member since:
    Jun 2014

    Posted 27 Feb 2015 Link to this post

    I did not notice that the Text property was being set to null.


    I was able to solve my problem by disallowing setting of null to my QueryText property.

    public string QueryText
    {
        get { return _queryText; }
        set
        {
            if(_queryText == value || value == null)
                return;
            _queryText = value;
     
            OnPropertyChanged();
        }
    }

    Additionally I also had to set CanAutocompleteSelectItems to False to avoid the Combobox automatically selectiong the item that was matching best to the QueryText.

    The xaml looks like this:
    <telerik:RadComboBox x:Name="RadComboBox" Text="{Binding QueryText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                         ItemsSource="{Binding RecentQueries}"
                         IsEditable="True"
                         IsFilteringEnabled="True"
                         TextSearchMode="Contains"
                         CanAutocompleteSelectItems="False"
                         OpenDropDownOnFocus="True"/>
  7. Nasko
    Admin
    Nasko avatar
    585 posts

    Posted 27 Feb 2015 Link to this post

    Hello Peter,

    We are glad that you've managed to achieve the desired. Please do not hesitate to contact us if you have any additional questions or concerns.

    Regards,
    Nasko
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
Back to Top
UI for WPF is Visual Studio 2017 Ready