Bind Full-Text-Search to custom TextBox?

17 posts, 0 answers
  1. Richard
    Richard avatar
    122 posts
    Member since:
    Feb 2009

    Posted 02 Jun 2016 Link to this post

    Is it possible to use a separate TextBox for the full text search? I'd would like to put the search text box in a toolbar on the UI.

    Thanks,
    Richard
  2. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 07 Jun 2016 Link to this post

    Hi Richard,

    The whole logic and initialization are inside the GridViewSearchPanel and its DataContext - SearchPanelViewModel. What could be done to get the behavior you want is to display the search panel initially so that the whole initialization logic is triggered and then bind the Text property of the TextBox you want to the SearchText property of the view model. It could be something like: 
    this.clubsGrid.ShowSearchPanel = false;
                var searchPanel = this.clubsGrid.ChildrenOfType<GridViewSearchPanel>().FirstOrDefault();
                this.txtBox.SetBinding(TextBox.TextProperty, new Binding("SearchText") { Source = searchPanel.DataContext, Mode = BindingMode.TwoWay });

    Please give it a try and let me know whether it corresponds to your requirements. 

    Regards,
    Maya
    Telerik
    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
  3. Kirk
    Kirk avatar
    1 posts
    Member since:
    Mar 2015

    Posted 26 Jul 2016 Link to this post

    I would also like to know if there is a simple way to bind to the Search Panel TextBox. I have several grids within a tab control that I would like to filter using a value that is bound to a TextBox outside of the tab control. 

    Is this possible?

    Thanks,

    Kirk

  4. Dilyan Traykov
    Admin
    Dilyan Traykov avatar
    1258 posts

    Posted 27 Jul 2016 Link to this post

    Hello Kirk,

    A simple way to achieve the desired behavior would be to handle the TextBox's TextChanged event and update the search criteria there:

    public MainWindow()
    {
        InitializeComponent();
     
        this.txtBox.TextChanged += TxtBox_TextChanged;
    }
     
    private void TxtBox_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
    {
        var searchPanels = this.ChildrenOfType<GridViewSearchPanel>();
        foreach (var searchPanel in searchPanels)
        {
            (searchPanel.DataContext as SearchViewModel).SearchText = this.txtBox.Text;
        }
    }

    Would such an approach be suitable for your requirements?

    Regards,
    Dilyan Traykov
    Telerik by Progress
    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
  5. Marcel
    Marcel avatar
    4 posts
    Member since:
    Nov 2015

    Posted 28 Jul 2016 in reply to Dilyan Traykov Link to this post

    That does not work directly on the binding data context, you get an exception:

    NullReferenceException: Waarde kan niet null zijn. Parameternaam: source (Value can not be null. Parameter: source)

       bij System.Linq.Enumerable.Any[TSource](IEnumerable`1 source, Func`2 predicate)
       bij Telerik.Windows.Controls.GridView.SearchPanel.SearchFilterDescriptorsManager.UpdateFilterDescriptors(List`1 searchValueInfos)
       bij Telerik.Windows.Controls.GridView.SearchPanel.SearchViewModel.set_SearchText(String value)
       bij [Name of app].View.MainWindow.<>c__DisplayClass60_0.<GeneralSearchBox_OnTextChanged>b__1() in [the source file]

     

  6. Marcel
    Marcel avatar
    4 posts
    Member since:
    Nov 2015

    Posted 28 Jul 2016 in reply to Marcel Link to this post

    It only works once you have shown the searchbox at least once. (Since then there is something bound to the datacontext).

    Would you know how to fix that without directly binding? To the textfield?

     

    Thanks,

    Marcel

  7. Dilyan Traykov
    Admin
    Dilyan Traykov avatar
    1258 posts

    Posted 29 Jul 2016 Link to this post

    Hello Marcel,

    As stated by my colleague in a previous post, the whole logic and initialization are inside the GridViewSearchPanel and its DataContext - the SearchPanelViewModel.

    So the only way to achieve the desired behavior is to display the search panel initially so that the whole initialization logic is triggered and hide it afterwards if you do not want it being shown.

    Regards,
    Dilyan Traykov
    Telerik by Progress
    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
  8. Josh
    Josh avatar
    4 posts
    Member since:
    Dec 2013

    Posted 19 Feb 2018 Link to this post

    Any news on this?  I want to use our own UI and not something baked in.   I don't want to show this UI initially.   I would like a more cleaner solution.  Can I construct the SearchViewModel?

     

     

  9. Dilyan Traykov
    Admin
    Dilyan Traykov avatar
    1258 posts

    Posted 21 Feb 2018 Link to this post

    Hello Josh,

    I'm afraid there's no other way to initialize the view model other than to use reflection which is not recommended.

    Please note that showing and immediately hiding the panel will also work in this case without this being obvious to the end user. Is there any specific reason you do not find such an approach applicable?

    Regards,
    Dilyan Traykov
    Progress Telerik
    Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
  10. shree
    shree avatar
    8 posts
    Member since:
    Feb 2018

    Posted 29 Jul 2020 in reply to Dilyan Traykov Link to this post

    Your solution works perfectly if there are only columns with string type data. I have datetime column as well and it is throwing exception for the same.
    Message is -   "The string was not recognized as a valid DateTime. There is an unknown word starting at index 0."

    Stack trace is -   at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles)

    What should I do for these datetime column please help. 

    I am using your code 

    *************

    var searchPanels = this.ChildrenOfType<GridViewSearchPanel>();
                foreach (var _searchPanel in searchPanels)
                {
                    (_searchPanel.DataContext as Telerik.Windows.Controls.GridView.SearchPanel.SearchViewModel).SearchText = this.SearchTextbox.Text;
                }

    ********

  11. Dilyan Traykov
    Admin
    Dilyan Traykov avatar
    1258 posts

    Posted 31 Jul 2020 Link to this post

    Hello shrinath,

    Thank you for the provided code snippet.

    I created a small sample project with it, however, the search functionality does not result in any exceptions at my end.

    Could you please try running the example and let me know how it differs from the project you've set up at your end? If possible, please modify the project so that the exception is observed and I will gladly try to suggest a possible solution. You can either upload the modified project to a storage provider of your choice or open a new support ticket and attach it there.

    Thank you in advance for your cooperation on the matter.

    Regards,
    Dilyan Traykov
    Progress Telerik

  12. shree
    shree avatar
    8 posts
    Member since:
    Feb 2018

    Posted 05 Aug 2020 in reply to Dilyan Traykov Link to this post

    Never mind, we have removed that column from our app.The code worked fine after I made the post.
    Thank you for replying back.
  13. Dilyan Traykov
    Admin
    Dilyan Traykov avatar
    1258 posts

    Posted 06 Aug 2020 Link to this post

    Hello shrinath,

    I'm happy to hear that you've managed to resolve your issue. If I can be of any further assistance, please let me know.

    Regards,
    Dilyan Traykov
    Progress Telerik

  14. shree
    shree avatar
    8 posts
    Member since:
    Feb 2018

    Posted 16 Sep 2020 in reply to Dilyan Traykov Link to this post

    Hi Dilyan,
    I have encountered one more issue with the approach. If we have string containing spaces as a search text, the outcome is all the words from search text are matched one by one and results are highlighted.
    However if you need to find the whole search text as one match from grid it doesn't provide the desired output.

    For example if search text is "011 of 2020" then all '011', 'of' and '2020' matches are highlighted, this way if "011 of 2020" is at the last row of grid and there thousands of rows then user will need to scroll to bottom.


    Can we modify this behaviour to search text "starts with" instead of "contains". I have attached screens from our app where exact "011 of 2020" is at last row of grid. one.jpg is top half and two.jpg is bottom half.
  15. shree
    shree avatar
    8 posts
    Member since:
    Feb 2018

    Posted 17 Sep 2020 in reply to shree Link to this post

    Solved by workaround -

    private void UserControl_Loaded(object sender, RoutedEventArgs e)<br>        {<br>            var searchPanel = this.navAreaListGrid.ChildrenOfType<GridViewSearchPanel>().FirstOrDefault();<br>            <br>            if (searchPanel != null)<br>            {<br>                SearchTextbox.SetBinding(TextBox.TextProperty, new Binding("SearchText") { Source = searchPanel.DataContext, Mode = BindingMode.OneTime });<br>                navAreaListGrid.ShowSearchPanel = false;<br>            }<br>        }<br><br>        private void WarningListTextSearch(object sender, TextChangedEventArgs args)<br>        {<br>            GridViewSearchPanel _searchPanel = navAreaListGrid.ChildrenOfType<GridViewSearchPanel>().FirstOrDefault();<br>            (_searchPanel.DataContext as Telerik.Windows.Controls.GridView.SearchPanel.SearchViewModel).SearchText = string.Format(@"""{0}""", SearchTextbox.Text); ;<br>}            
  16. shree
    shree avatar
    8 posts
    Member since:
    Feb 2018

    Posted 17 Sep 2020 in reply to shree Link to this post

    01.private void UserControl_Loaded(object sender, RoutedEventArgs e)
    02. {
    03.  var searchPanel = this.radGrid.ChildrenOfType<GridViewSearchPanel>().FirstOrDefault();
    04.             
    05. if (searchPanel != null)
    06. {
    07.   SearchTextbox.SetBinding(TextBox.TextProperty, new Binding("SearchText") { Source =                         searchPanel.DataContext, Mode = BindingMode.OneTime });
    08.   radGrid.ShowSearchPanel = false;
    09.}
    10. }
    11. 
    12. private void WarningListTextSearch(object sender, TextChangedEventArgs args)
    13. {
    14.    GridViewSearchPanel _searchPanel =
    15.             radGrid.ChildrenOfType<GridViewSearchPanel>().FirstOrDefault();
    16.    (_searchPanel.DataContext as
    17.        Telerik.Windows.Controls.GridView.SearchPanel.SearchViewModel).SearchText =
    18.                  string.Format(@"""{0}""", SearchTextbox.Text); ;
    19. }
  17. Dilyan Traykov
    Admin
    Dilyan Traykov avatar
    1258 posts

    Posted 18 Sep 2020 Link to this post

    Hi shree,

    I'm glad to hear that you've found a suitable workaround and would like to thank you for sharing it with our community.

    I just want to add that you can also use the MatchAllTerms SearchMode which should provide the same result.

    Regards,
    Dilyan Traykov
    Progress Telerik

    Five days of Blazor, Angular, React, and Xamarin experts live-coding on twitch.tv/CodeItLive, special prizes, and more, for FREE?! Register now for DevReach 2.0(20).

Back to Top