Access PART_SearchPanel DataContext From HighLightTextBlock

3 posts, 0 answers
  1. Roberto
    Roberto avatar
    4 posts
    Member since:
    Dec 2017

    Posted 23 May Link to this post

    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. Dinko
    Admin
    Dinko avatar
    937 posts

    Posted 28 May Link to this post

    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.
  3. Roberto
    Roberto avatar
    4 posts
    Member since:
    Dec 2017

    Posted 28 May in reply to Dinko Link to this post

    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.

     

Back to Top