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

Access PART_SearchPanel DataContext From HighLightTextBlock

2 Answers 109 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Roberto
Top achievements
Rank 1
Roberto asked on 23 May 2019, 11:12 AM

Hello.

Can I access SearchPanel DataContext from another part of Template?

I have a HightLightningTextBlock Class that I want to use instead of the HighLightTextBlock used in DataColumn. I use a DataTemplate:

<DataTemplate x:Key="HighlightingTemplate">
    <highLight:HighlightingTextBlock HighlightText="{Binding SearchViewModel.SearchText}" Text="{Binding Name}">
        <i:Interaction.Behaviors>
            <highLigthBehavior:CellValueBindingBehavior />
        </i:Interaction.Behaviors>
    </highLight:HighlightingTextBlock>
</DataTemplate>

I asign the DataTemplate when adding a column:

column.CellTemplate = (DataTemplate)Application.Current.Resources["HighlightingTemplate"];

I want to bind the "HighlightText" property to the "SearchText" of "PART_SearchAsYouTypeTextBox". The "Text" property works well. I use the HighlightingTextBlock because I want to change the background instead of the foreground when type in search box.

Thank you.

2 Answers, 1 is accepted

Sort by
0
Dinko | Tech Support Engineer
Telerik team
answered on 28 May 2019, 08:36 AM
Hi Roberto,

Thank you for the provided code snippet.

Let me start with that the default HighlightTextBlock is applied internally to the text when the user types something in the search panel. So setting your custom HighlightingTextBlock in the CellTemplate won't work out of the box. What I can suggest here is to create a custom column and add the HighlightingTextBlock inside it. You can check the HighlightCustomColumn SDK Example in our GitHub repository which demonstrates this. Can you check this example and let me know if it works for you?

Regards,
Dinko
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Roberto
Top achievements
Rank 1
answered on 28 May 2019, 10:01 AM

Thanks for your reply.

I needed to use the HighLightTextBlock in all columns. I use a sample that I found in https://www.jeff.wilcox.name/2009/08/sl3-highlighting-text-block/.

I solved the binding to HighlightText changing the override method OnApplyTemplate() and now this work for me.

Template:

<Style TargetType="highlight:HighlightingTextBlock">
    <Setter Property="HighlightBrush" Value="Blue" />
    <Setter Property="HighlightBackgroundBrush" Value="Yellow" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="highlight:HighlightingTextBlock">
                <TextBlock x:Name="Text" />
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
<DataTemplate x:Key="HighlightingTemplate">
    <highlight:HighlightingTextBlock>
        <i:Interaction.Behaviors>
            <highligthBehavior:CellValueBindingBehavior />
        </i:Interaction.Behaviors>
    </highlight:HighlightingTextBlock>
</DataTemplate>

Apply the template when adding a column:

column.CellTemplate = (DataTemplate)Application.Current.Resources["HighlightingTemplate"];
dataGrid.Columns.Add(column);

And change the override Method OnApplyTemplate:

/// <summary>
/// Override the apply template handler.
/// </summary>
public override void OnApplyTemplate()
{
    //base.OnApplyTemplate();
 
    //// Grab the template part
 
    //// Re-apply the text value
    //string text = Text;
    //Text = null;
    //Text = text;
    base.OnApplyTemplate();
 
    // Grab the template part
    TextBlock = GetTemplateChild(TextBlockName) as TextBlock;
 
    // Re-apply the text value
    string text = Text;
    Text = null;
    Text = text;
 
    // Bind HighLightTextProperty
    var grid = TreeHelper.TryFindParent<RadGridView>(this);
    if (grid != null)
    {
        var template = grid.Template;
        var gridViewSearchPanel = (GridViewSearchPanel)template.FindName("PART_SearchPanel", grid);
        if (gridViewSearchPanel != null)
        {
            Binding binding = new Binding
            {
                Source = gridViewSearchPanel.DataContext,
                Path = new PropertyPath("SearchText"),
                Mode = BindingMode.OneWay,
                UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
            };
            BindingOperations.SetBinding(this, HighlightTextProperty, binding);
        }
    }
}

I attach the result.

Thank you.

 

Tags
GridView
Asked by
Roberto
Top achievements
Rank 1
Answers by
Dinko | Tech Support Engineer
Telerik team
Roberto
Top achievements
Rank 1
Share this question
or