How to add a ToolTipService.ToolTip with CellStyleSelector?

2 posts, 0 answers
  1. Scott
    Scott avatar
    16 posts
    Member since:
    Oct 2013

    Posted 29 Jan 2014 Link to this post

    I've got the following GridViewDataColumn:

    1.<telerik:GridViewDataColumn
    2.    Header="Oldest Balance"
    3.    DataMemberBinding="{Binding OldestBalance, Converter={StaticResource Currency2Converter}}"
    4.    CellStyleSelector="{StaticResource OldestBalanceColorStyleSelector}"
    5.    HeaderCellStyle="{StaticResource rgvHeaderCellStyle}"
    6.    HeaderTextAlignment="Center"
    7.    TextAlignment="Right"
    8.    Width="Auto" />


    The OldestBalanceStyleSelector is as follows:

    01.public class OldestBalanceColorStyleSelector : Telerik.Windows.Controls.StyleSelector
    02.{
    03.    public override Style SelectStyle(object item, DependencyObject container)
    04.    {
    05.        var cpData = item as CreditPolicyData;
    06.        var cell = container as GridViewCell;
    07.        var style = new Style(typeof(GridViewCell));
    08. 
    09.        style.Setters.Add(new Setter
    10.        (
    11.            GridViewCell.BorderBrushProperty,
    12.            new SolidColorBrush(Colors.Transparent)
    13.        ));
    14.        style.Setters.Add(new Setter
    15.        (
    16.            GridViewCell.BorderThicknessProperty,
    17.            new Thickness(0)
    18.        ));
    19.        style.Setters.Add(new Setter
    20.        (
    21.            GridViewCell.MarginProperty,
    22.            new Thickness(0)
    23.        ));
    24.        style.Setters.Add(new Setter
    25.        (
    26.            GridViewCell.PaddingProperty,
    27.            new Thickness(20, 10, 20, 10)
    28.        ));
    29. 
    30.        if
    31.        (
    32.            cpData.IsNotNull() &&
    33.            cpData.OldestBalanceColor.IsNotNullOrEmpty()
    34.        )
    35.        {
    36.            switch (cpData.OldestBalanceColor.ToLower())
    37.            {
    38.                case "red":
    39.                    style.Setters.Add(new Setter
    40.                    (
    41.                        GridViewCell.BackgroundProperty,
    42.                        new SolidColorBrush(Color.FromArgb(255, 214, 50, 50))
    43.                    ));
    44.                    style.Setters.Add(new Setter
    45.                    (
    46.                        GridViewCell.ForegroundProperty,
    47.                        new SolidColorBrush(Colors.White)
    48.                    ));
    49.                    break;
    50. 
    51.                case "yellow":
    52.                    style.Setters.Add(new Setter
    53.                    (
    54.                        GridViewCell.BackgroundProperty,
    55.                        new SolidColorBrush(Color.FromArgb(255, 255, 238, 98))
    56.                    ));
    57.                    style.Setters.Add(new Setter
    58.                    (
    59.                        GridViewCell.ForegroundProperty,
    60.                        new SolidColorBrush(Colors.Black)
    61.                    ));
    62.                    break;
    63.                         
    64.                case "black":
    65.                    style.Setters.Add(new Setter
    66.                    (
    67.                        GridViewCell.BackgroundProperty,
    68.                        new SolidColorBrush(Color.FromArgb(255, 34, 40, 43))
    69.                    ));
    70.                    style.Setters.Add(new Setter
    71.                    (
    72.                        GridViewCell.ForegroundProperty,
    73.                        new SolidColorBrush(Colors.White)
    74.                    ));
    75.                    break;
    76. 
    77.                default:
    78.                    style.Setters.Add(new Setter
    79.                    (
    80.                        GridViewCell.BackgroundProperty,
    81.                        new SolidColorBrush(Colors.Transparent)
    82.                    ));
    83.                    break;
    84.            }
    85.        }
    86.        else
    87.        {
    88.            style.Setters.Add(new Setter
    89.            (
    90.                GridViewCell.BackgroundProperty,
    91.                new SolidColorBrush(Colors.Transparent)
    92.            ));
    93.        }
    94. 
    95.        return style;
    96.    }
    97.}


    I need to add a ToolTip that describes what the color means.  Typically, I'd do something like this in XAML:

    01.<telerik:GridViewColumn.CellStyle>
    02.  <Style
    03.    BasedOn="{StaticResource rgvCellStyle}"
    04.    TargetType="telerik:GridViewCell">
    05. 
    06.    <Setter Property="Template">
    07.      <Setter.Value>
    08.        <ControlTemplate TargetType="telerik:GridViewCell">
    09. 
    10.          <Border
    11.            Background="{TemplateBinding Background}"
    12.            BorderBrush="{TemplateBinding BorderBrush}"
    13.            BorderThickness="{TemplateBinding BorderThickness}"
    14.            ToolTipService.Placement="Bottom"
    15.            ToolTipService.ToolTip="{Binding OldestBalanceColorText}">
    16. 
    17.            <ContentPresenter />
    18. 
    19.          </Border>
    20. 
    21.        </ControlTemplate>
    22.      </Setter.Value>
    23.    </Setter>
    24. 
    25.  </Style>
    26.</telerik:GridViewColumn.CellStyle>


    However, this isn't showing correctly.  Am I doing the ContentPresenter part wrong?  Can this be accomplished in the CellStyleSelector at the same time I'm setting the color of the background?  This would be ideal, as I wouldn't need a converter or anything, I'd just set the text statically within that switch statement.
  2. Scott
    Scott avatar
    16 posts
    Member since:
    Oct 2013

    Posted 29 Jan 2014 in reply to Scott Link to this post

    I've figured it out.  

    I ended up using cell.SetValue(ToolTipService.ToolTipProperty, "text"); for the tooltip text and cell.SetValue(ToolTipService.PlacementProperty, PlacementMode.Bottom); for the placement of the text.

    Here's how it modified my code:

    001.public class OldestBalanceColorStyleSelector : Telerik.Windows.Controls.StyleSelector
    002.{
    003.    public override Style SelectStyle(object item, DependencyObject container)
    004.    {
    005.        var cpData = item as CreditPolicyData;
    006.        var cell = container as GridViewCell;
    007.        var style = new Style(typeof(GridViewCell));
    008. 
    009.        style.Setters.Add(new Setter
    010.        (
    011.            GridViewCell.BorderBrushProperty,
    012.            new SolidColorBrush(Colors.Transparent)
    013.        ));
    014. 
    015.        style.Setters.Add(new Setter
    016.        (
    017.            GridViewCell.BorderThicknessProperty,
    018.            new Thickness(0)
    019.        ));
    020. 
    021.        style.Setters.Add(new Setter
    022.        (
    023.            GridViewCell.MarginProperty,
    024.            new Thickness(0)
    025.        ));
    026. 
    027.        style.Setters.Add(new Setter
    028.        (
    029.            GridViewCell.PaddingProperty,
    030.            new Thickness(20, 10, 20, 10)
    031.        ));
    032. 
    033.        if
    034.        (
    035.            cpData.IsNotNull() &&
    036.            cpData.OldestBalanceColor.IsNotNullOrEmpty()
    037.        )
    038.        {
    039.            cell.SetValue
    040.            (
    041.                ToolTipService.PlacementProperty,
    042.                PlacementMode.Bottom
    043.            );
    044. 
    045.            switch (cpData.OldestBalanceColor.ToLower())
    046.            {
    047.                case "red":
    048. 
    049.                    cell.SetValue
    050.                    (
    051.                        ToolTipService.ToolTipProperty,
    052.                        "Is over 11 months old."
    053.                    );
    054. 
    055.                    style.Setters.Add(new Setter
    056.                    (
    057.                        GridViewCell.BackgroundProperty,
    058.                        new SolidColorBrush(Color.FromArgb(255, 214, 50, 50))
    059.                    ));
    060. 
    061.                    style.Setters.Add(new Setter
    062.                    (
    063.                        GridViewCell.ForegroundProperty,
    064.                        new SolidColorBrush(Colors.White)
    065.                    ));
    066. 
    067.                    break;
    068. 
    069.                case "yellow":
    070. 
    071.                    cell.SetValue
    072.                    (
    073.                        ToolTipService.ToolTipProperty,
    074.                        "Is 8 to 10 months old."
    075.                    );
    076. 
    077.                    style.Setters.Add(new Setter
    078.                    (
    079.                        GridViewCell.BackgroundProperty,
    080.                        new SolidColorBrush(Color.FromArgb(255, 255, 238, 98))
    081.                    ));
    082. 
    083.                    style.Setters.Add(new Setter
    084.                    (
    085.                        GridViewCell.ForegroundProperty,
    086.                        new SolidColorBrush(Colors.Black)
    087.                    ));
    088. 
    089.                    break;
    090.                         
    091.                case "black":
    092. 
    093.                    cell.SetValue
    094.                    (
    095.                        ToolTipService.ToolTipProperty,
    096.                        "Is 1 to 7 months old."
    097.                    );
    098. 
    099.                    style.Setters.Add(new Setter
    100.                    (
    101.                        GridViewCell.BackgroundProperty,
    102.                        new SolidColorBrush(Color.FromArgb(255, 34, 40, 43))
    103.                    ));
    104. 
    105.                    style.Setters.Add(new Setter
    106.                    (
    107.                        GridViewCell.ForegroundProperty,
    108.                        new SolidColorBrush(Colors.White)
    109.                    ));
    110. 
    111.                    break;
    112. 
    113.                default:
    114. 
    115.                    style.Setters.Add(new Setter
    116.                    (
    117.                        GridViewCell.BackgroundProperty,
    118.                        new SolidColorBrush(Colors.Transparent)
    119.                    ));
    120. 
    121.                    break;
    122.            }
    123.        }
    124.        else
    125.        {
    126.            style.Setters.Add(new Setter
    127.            (
    128.                GridViewCell.BackgroundProperty,
    129.                new SolidColorBrush(Colors.Transparent)
    130.            ));
    131.        }
    132. 
    133.        return style;
    134.    }
    135.}

  3. DevCraft banner
Back to Top