Change color of highlighted items in RadGanttView

1 Answer 19 Views
GanttView
Jennifer
Top achievements
Rank 1
Iron
Jennifer asked on 25 Mar 2024, 11:23 PM | edited on 25 Mar 2024, 11:25 PM

We would like to change the color of the highlighted items (i.e., HighlightedItemsSource) in the RadGanttView.

I know this is possible by copying and customizing the ControlTemplate, but I would like to avoid that if possible since I would need to do that for all the different types of containers.

I also know it's possible to change the color in the theme (we're using Office2019), but unfortunately, the template uses ValidationBrush for these highlights and we still want the validation to show as red in other areas of our application.

Is there any other way to change the color of the highlighted items?

1 Answer, 1 is accepted

Sort by
0
Stenly
Telerik team
answered on 26 Mar 2024, 12:31 PM

Hello Jennifer,

To achieve this requirement, without extracting the default control templates of the event containers, a custom attached property can be defined, which will retrieve the Border element using the ChildrenOfType extension method and modify its Background and BorderBrush properties.

The following code snippets show the above suggestion's implementation:

public class EventContainerExtensions
{
    public static SolidColorBrush GetEventContainerHighlightBrush(DependencyObject obj)
    {
        return (SolidColorBrush)obj.GetValue(EventContainerHighlightBrushProperty);
    }

    public static void SetEventContainerHighlightBrush(DependencyObject obj, SolidColorBrush value)
    {
        obj.SetValue(EventContainerHighlightBrushProperty, value);
    }

    public static readonly DependencyProperty EventContainerHighlightBrushProperty =
        DependencyProperty.RegisterAttached("EventContainerHighlightBrush", typeof(SolidColorBrush), typeof(EventContainerExtensions), new PropertyMetadata(null, OnEventContainerHighlightBrushChanged));

    private static void OnEventContainerHighlightBrushChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        EventContainer eventContainer = (EventContainer)d;

        eventContainer.Loaded += EventContainer_Loaded;
    }

    private static void EventContainer_Loaded(object sender, RoutedEventArgs e)
    {
        EventContainer eventContainer = sender as EventContainer;

        if (eventContainer != null)
        {
            SolidColorBrush newColor = GetEventContainerHighlightBrush(eventContainer);

            //SummaryContainer
            if (eventContainer is SummaryContainer summaryContainer)
            {
                ChangeHighlightColorOfSummaryContainer(summaryContainer, newColor);
            }
            //EventContainer
            else
            {
                ChangHighlightColorOfEventContainer(eventContainer, newColor);
            }
        }
    }

    private static void ChangHighlightColorOfEventContainer(EventContainer eventContainer, SolidColorBrush newColor)
    {
        Border highlightVisual = eventContainer.ChildrenOfType<Border>().FirstOrDefault(x => x.Name == "HighlightVisual");

        if (highlightVisual != null)
        {
            highlightVisual.Background = newColor;
            highlightVisual.BorderBrush = newColor;
        }
    }

    private static void ChangeHighlightColorOfSummaryContainer(EventContainer summaryContainer, SolidColorBrush newColor)
    {
        Border backgroundBorderHighlighted = summaryContainer.ChildrenOfType<Border>().FirstOrDefault(x => x.Name == "BackgroundBorderHighlighted");
        Path rightMarkerHighlighted = summaryContainer.ChildrenOfType<Path>().FirstOrDefault(x => x.Name == "RightMarkerHighlighted");
        Path leftMarkerHighlighted = summaryContainer.ChildrenOfType<Path>().FirstOrDefault(x => x.Name == "LeftMarkerHighlighted");

        if (backgroundBorderHighlighted != null && rightMarkerHighlighted != null && leftMarkerHighlighted != null)
        {
            backgroundBorderHighlighted.Background = newColor;
            backgroundBorderHighlighted.BorderBrush = newColor;

            rightMarkerHighlighted.Fill = newColor;
            leftMarkerHighlighted.Fill = newColor;
        }
    }
}

Registering the custom attached property:

<Grid.Resources>
    <Style TargetType="telerik:EventContainer">
        <Setter Property="local:EventContainerExtensions.EventContainerHighlightBrush" Value="Orange"/>
    </Style>
    <Style TargetType="telerik:SummaryContainer">
        <Setter Property="local:EventContainerExtensions.EventContainerHighlightBrush" Value="Orange"/>
    </Style>
</Grid.Resources>

The produced result is as follows:

With this being said, could you give this suggestion a try?

Regards,
Stenly
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
GanttView
Asked by
Jennifer
Top achievements
Rank 1
Iron
Answers by
Stenly
Telerik team
Share this question
or