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

Search as you type - making custom columns highlighted

8 Answers 321 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Inger Marie
Top achievements
Rank 1
Inger Marie asked on 09 Feb 2017, 01:11 PM

I have a custom column, which contains a textbox when in editing mode, and two textblocks when not in editing mode. These controls are created through CreateCellElement and CreatedCellEditElement in my custom column.

The first textblock (A) shows the text - and the second textblock (B) shows a status.

I also have the search as you type on the RadGridView (ShowSearchPanel=True). It works for my custom column (I have bound the datamemberbinding to the text in the viewmodel). But the text in the textblock (A) does not get hightlighted.

The user may enter several lines of text, but textblock A will only show the first line (through alignments) - this is done to keep the rows from becomming to high when users enters a lot of text.

 

Is there any way to have the column highlight, when there is a match in Search as you type?

8 Answers, 1 is accepted

Sort by
0
Stefan Nenchev
Telerik team
answered on 14 Feb 2017, 09:42 AM
Hi Inger Marie,

Please review the Custom Highlight Column demo in our SDK Samples Browser as it implements a behavior similar to the one you are looking for. You can also find the project on GitHub.

Regards,
Stefan Nenchev
Telerik by Progress
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
Inger Marie
Top achievements
Rank 1
answered on 22 Feb 2017, 12:01 PM

Thanks.

I downloaded the sample and had it running and highlight the text in the column.That was good.

Then I tried to code that in my own - and failed.

Then I added my custom column (GridViewNoteColumn) to the samle solution, and also NoteTextBlock which is used by GridViewNoteColumn. The NoteTextBlock inherits from HighlightTextBlock.It still does not work - and I cannot figure out why.

Best regards

Inger Marie

0
Inger Marie
Top achievements
Rank 1
answered on 22 Feb 2017, 12:03 PM

Here is the main code:

public override FrameworkElement CreateCellElement(GridViewCell cell, object dataItem)
    {
        if (cell.Content != null)
            return cell.Content as Grid;
 
        if (DataMemberBinding == null)
            return null;
 
        //Grid contentGrid = cell.Content as Grid ?? CreateCellContent(cell);
        //return contentGrid;
 
        var gridView = DataControl;
        Grid result = new Grid
        {
            HorizontalAlignment = HorizontalAlignment.Stretch,
            VerticalAlignment = VerticalAlignment.Stretch
        };
        result.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
        result.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Auto) });
 
        // Note TextBlock
        NoteTextBlock noteTextBlock = new NoteTextBlock();
        noteTextBlock.SetValue(Grid.ColumnProperty, 0);
        noteTextBlock.HorizontalAlignment = HorizontalAlignment.Stretch;
        noteTextBlock.VerticalAlignment = VerticalAlignment.Stretch;
        noteTextBlock.TextAlignment = TextAlignment.Left;
        noteTextBlock.TextTrimming = TextTrimming.None;
        noteTextBlock.TextWrapping = TextWrapping.Wrap;
        noteTextBlock.LineStackingStrategy = LineStackingStrategy.MaxHeight;
 
        string rowHeightPath = nameof(gridView.RowHeight);
        noteTextBlock.SetBinding(FrameworkElement.MaxHeightProperty, new Binding { Source = gridView, Path = new PropertyPath(rowHeightPath) });
        noteTextBlock.SetBinding(TextBlock.TextProperty, new Binding(DataMemberBinding.Path.Path)); // { NotifyOnTargetUpdated = true });
 
        // Highlighting: Making sure the search field in radgridview can highlight text in this column
        noteTextBlock.DataType = DataType; // typeof(String);
        noteTextBlock.SetBinding(HighlightTextBlock.HighlightTextProperty, new Binding(DataMemberBinding.Path.Path));
        cell.SetBinding(GridViewCell.IsHighlightedProperty, new Binding(nameof(NoteTextBlock.ContainsMatch)) { Source = noteTextBlock, Mode = BindingMode.TwoWay });
        // TODO: I want the row to become heigher and show the entire text if I succeed at highlighting it
        //noteTextBlock.PropertyChanged += NoteTextBlock_PropertyChanged;
 
        result.Children.Add(noteTextBlock);
 
        // trimming textblock (the dots)
        TextBlock trimTextBlock = new TextBlock();
        trimTextBlock.SetValue(Grid.ColumnProperty, 1);
        trimTextBlock.Margin = new Thickness(1, 0, 0, 0);
        trimTextBlock.HorizontalAlignment = HorizontalAlignment.Right;
        trimTextBlock.VerticalAlignment = VerticalAlignment.Bottom;
        trimTextBlock.Text = "\u2026";
 
        //NoteTrimmedVisibility
        Binding visibilityBinding = new Binding
        {
            Source = noteTextBlock,
            Path = new PropertyPath(NoteTextBlock.TrimmedVisibilityProperty)
        };
        trimTextBlock.SetBinding(UIElement.VisibilityProperty, visibilityBinding);
        result.Children.Add(trimTextBlock);
        return result;
    }
0
Inger Marie
Top achievements
Rank 1
answered on 23 Feb 2017, 07:08 AM

I took an extra look at the problem yesterday. This is what I found:

The highlightning does not work on my custom column because of this line in CreateCellElement():

noteTextBlock.SetBinding(TextBlock.TextProperty, new Binding(DataMemberBinding.Path.Path));

 

(NoteTextBlock is a custom textblock that inherits from HighlightTextBlock)

If I remove that line and insert the call to SetHighlightTextBlockTextProperty() (Which uses SetValue and GetValue and ClearValue to set the Text through TextProperty), then my application fails with System.NullReferenceException in PresentationFramework.dll. I attached the top rows from the stack trace.

My custom textblock does not used the textbinding - so I think it is HighlightTextBlock that causes the problem. I can send you a small project which contains the problem.

I fixed it by doing this:

// In the top, I removed the reuse of cell.Content:
 //if (cell.Content != null)
 //    return cell.Content as Grid;
 
...
 
Binding dummyBinding = new Binding();
noteTextBlock.SetBinding(TextBlock.TextProperty, dummyBinding);
SetHighlightTextBlockTextProperty(dataItem, noteTextBlock);
 
...

 

Which is bad cod:

 

1. I would love to have the reuse of cell.Content - because that would prohibit memory leaks due to event hookups (I do not know which method to overwrite in order to clean up the old cell content before CreateCellElement gets called).

2. Having dummy stuff is never a good idea.

So I would be happy if you would help fix the problem with the ReferenceNullException. I can send you a zip-file with a small project that has the problem.

Thanks,

Inger Marie

 

 

0
Stefan Nenchev
Telerik team
answered on 24 Feb 2017, 09:32 AM
Hi Inger Marie,

Would it be convenient to provide the sample so we can have a more detailed look?
You can raise a ticket and attach it or use some cloud platform and provide the link here. 

Thanks.

Regards,
Stefan Nenchev
Telerik by Progress
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 you to write beautiful native mobile apps using a single shared C# codebase.
0
Inger Marie
Top achievements
Rank 1
answered on 24 Feb 2017, 09:50 AM

Here is a link:

Link to zip file

I  later discovered that when I tried to use the "bad" solution (which works in the samle project) it did not work in my real life project. All columns became the name of the ViewModel class (which does not have a ToString() because that does not make sense).

0
Accepted
Stefan Nenchev
Telerik team
answered on 28 Feb 2017, 01:32 PM
Hello Inger Marie,

Thank you for the sample attached. Indeed, I was able to reproduce the Null Reference exception that you have reported "If I remove that line and insert the call to SetHighlightTextBlockTextProperty() (Which uses SetValue and GetValue and ClearValue to set the Text through TextProperty), then my application fails with System.NullReferenceException in PresentationFramework.dll" .

After some further investigation, it seems that the exception was caused due to the logic you have applied in the static constructor of the NoteTextBlock element:

TextProperty.OverrideMetadata(typeof(NoteTextBlock), new FrameworkPropertyMetadata(default(string), TextChangedCallback));

You can apply the following as it seems to work as expected:

TextProperty.OverrideMetadata(typeof(NoteTextBlock), new FrameworkPropertyMetadata(string.Empty, TextChangedCallback));

Please give it a try in your actual application.

Regards,
Stefan Nenchev
Telerik by Progress
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 you to write beautiful native mobile apps using a single shared C# codebase.
0
Inger Marie
Top achievements
Rank 1
answered on 01 Mar 2017, 01:20 PM

It works. Thanks!

It's weird that I cannot have null values - but it does not seem to affect the saved data.

Best regards

Inger Marie

Tags
GridView
Asked by
Inger Marie
Top achievements
Rank 1
Answers by
Stefan Nenchev
Telerik team
Inger Marie
Top achievements
Rank 1
Share this question
or