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

Bind Full-Text-Search to custom TextBox?

16 Answers 873 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Richard
Top achievements
Rank 2
Iron
Veteran
Iron
Richard asked on 02 Jun 2016, 07:38 PM
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

16 Answers, 1 is accepted

Sort by
1
Maya
Telerik team
answered on 07 Jun 2016, 01:13 PM
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.
0
Kirk
Top achievements
Rank 1
answered on 26 Jul 2016, 09:48 AM

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

0
Dilyan Traykov
Telerik team
answered on 27 Jul 2016, 02:12 PM
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.
0
Marcel
Top achievements
Rank 1
answered on 28 Jul 2016, 09:48 AM

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]

 

0
Marcel
Top achievements
Rank 1
answered on 28 Jul 2016, 09:53 AM

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

0
Dilyan Traykov
Telerik team
answered on 29 Jul 2016, 08:47 AM
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.
0
Josh
Top achievements
Rank 1
answered on 19 Feb 2018, 09:09 PM

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?

 

 

0
Dilyan Traykov
Telerik team
answered on 21 Feb 2018, 04:10 PM
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.
0
shree
Top achievements
Rank 1
Veteran
answered on 29 Jul 2020, 11:37 AM

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;
            }

********

0
Dilyan Traykov
Telerik team
answered on 31 Jul 2020, 02:02 PM

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

0
shree
Top achievements
Rank 1
Veteran
answered on 05 Aug 2020, 07:55 PM
Never mind, we have removed that column from our app.The code worked fine after I made the post.
Thank you for replying back.
0
Dilyan Traykov
Telerik team
answered on 06 Aug 2020, 02:53 PM

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

0
shree
Top achievements
Rank 1
Veteran
answered on 16 Sep 2020, 05:49 PM
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.
0
shree
Top achievements
Rank 1
Veteran
answered on 17 Sep 2020, 01:35 PM
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>}            
0
shree
Top achievements
Rank 1
Veteran
answered on 17 Sep 2020, 01:46 PM
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. }
0
Dilyan Traykov
Telerik team
answered on 18 Sep 2020, 11:04 AM

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).

Tags
GridView
Asked by
Richard
Top achievements
Rank 2
Iron
Veteran
Iron
Answers by
Maya
Telerik team
Kirk
Top achievements
Rank 1
Dilyan Traykov
Telerik team
Marcel
Top achievements
Rank 1
Josh
Top achievements
Rank 1
shree
Top achievements
Rank 1
Veteran
Share this question
or