Setting the current cell border to inactive color when GridView looses focus

3 posts, 0 answers
  1. Arthur
    Arthur avatar
    22 posts
    Member since:
    Jul 2014

    Posted 18 Nov 2014 Link to this post

    Hi,

    I have a styling question:

    When a GridView looses focus it is possible to set the background of a GridViewRow to a color that indicates, that the selection is inactive (like Selector.IsSelectionActive). Now at the same time, I want to set the Background_Current border of a GridViewCell to an inactive color. Because there is no VisualState like "CurrentUnfocused" on a GridViewCell, I do not know how to achieve this.

    I thought about binding somehow to the state of the parent row, but could not find a way. Any ideas?

    Thx
  2. Arthur
    Arthur avatar
    22 posts
    Member since:
    Jul 2014

    Posted 18 Nov 2014 Link to this post

    Finally I found a solution:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using Telerik.Windows.Controls.GridView;
     
    namespace XXX
    {
        public static class RowSelectionActiveProvider
        {
            public static readonly DependencyProperty IsRowSelectionActiveProperty =
                DependencyProperty.RegisterAttached(
                    "IsRowSelectionActive",
                    typeof(bool),
                    typeof(RowSelectionActiveProvider),
                    new PropertyMetadata(false));
     
            public static Boolean GetIsRowSelectionActive(GridViewRow target)
            {
                if (target == null)
                    return false;
     
                return (Boolean)target.GetValue(IsRowSelectionActiveProperty);
            }
     
            public static void SetIsRowSelectionActive(GridViewRow target, Boolean value)
            {
                if (target != null)
                {
                    target.SetValue(IsRowSelectionActiveProperty, value);
                }
            }
        }
    }

    In the GridViewRow style:

    <VisualState x:Name="Selected">
        <Storyboard>
            <BooleanAnimationUsingKeyFrames
                         Storyboard.TargetProperty="(local:RowSelectionActiveProvider.IsRowSelectionActive)"
                         Storyboard.TargetName="PART_DataCellsPresenter">
                        <DiscreteBooleanKeyFrame Value="True" KeyTime="0"/>
                     </BooleanAnimationUsingKeyFrames>
            </Storyboard>
    </VisualState>

    In the GridViewCell style:
    <ControlTemplate.Triggers>
        <DataTrigger Binding="{Binding (local:RowSelectionActiveProvider.IsRowSelectionActive), RelativeSource={RelativeSource AncestorType={x:Type telerik:DataCellsPresenter}}}"
                 Value="False">
            <Setter TargetName="Background_Current"
                            Property="BorderBrush"
                             Value="Gray"/>
            </DataTrigger>
    </ControlTemplate.Triggers>

    Puh, hours wasted, but it works.

    Thanks
  3. Vanya Pavlova
    Admin
    Vanya Pavlova avatar
    2019 posts

    Posted 19 Nov 2014 Link to this post

    Hi Arthur,


    I'm glad to hear you were able to find an appropriate solution for your case.
    Have a great day. 


    Regards,
    Vanya Pavlova
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
Back to Top