Binding CellStyle in a RadGridView

4 posts, 1 answers
  1. Juan
    Juan avatar
    9 posts
    Member since:
    Jul 2012

    Posted 02 Sep 2010 Link to this post

    I have a GridView where for certain the values are Nullable Booleans, when the value happens to be null, the controls renders a CheckBox with the IsTrueState property to true, presenting a CheckBox with a minus sign on it, something I feel is quite unnoticeable, so I decided to color the cell with a background color but only when the condition of being null is met. the problem is that the binding seems to be ignored by the control

    I have the following code: For the Value Converter.

    public class TriStateBooleanToStyleValueConverter : IValueConverter
    {
    public object Convert(object value, 
    Type targetType, 
    object parameter, 
    System.Globalization.CultureInfo culture)
    {
    ResourceDictionary converterDictionary = new ResourceDictionary()

    Source = new Uri("/SilverlightApplication1;component/Assets/Styles.xaml"UriKind.RelativeOrAbsolute) 
    };

    if
     (value == null)
    {
    return converterDictionary["NullCellRedStyle"as Style;
    }
    else
    {
    return converterDictionary["NullCellWhiteStyle"as Style;
    }
    }

    public
     object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
    throw new NotImplementedException();
    }
    }

     







    For the Styles defined in Styles.xaml

    <Style x:Key="NullCellRedStyle" TargetType="telerik:GridViewCell">
    <Setter Property="Background" Value="#FFFFD8D8"/>
    </Style>

    <Style x:Key="NullCellWhiteStyle" TargetType="telerik:GridViewCell">
    <Setter Property="Background" Value="#FFFFFFFF"/>
    </Style>
    Also have the Converter defined as a resource in the same styles file
    <converters:TriStateBooleanToStyleValueConverter x:Key="TriBoolToStyle"/>
    At the column level I have the following:
    <telerik:GridViewDataColumn Header="To Pay" MinWidth="70" Width="90" DataMemberBinding="{Binding Path=ToPay}" CellStyle="{Binding Path=ToPay, Converter={StaticResource TriBoolToStyle}}" />
    I define the CellStyle directly without binding and it works, also, I placed a breakpoint into the converter and it is never reached.
    Thanks
    Juan Mejia
  2. Answer
    Vlad
    Admin
    Vlad avatar
    11100 posts

    Posted 03 Sep 2010 Link to this post

    Hello,

     You cannot use Binding in this case since the context of the column is the context of the grid it self - not the data item. You can put desired control in CellTemplate and apply the same binding - in this case everything will work as expected. It will be better however if you use our CellStyleSelector instead - it will be easier.

    Kind regards,
    Vlad
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. Juan
    Juan avatar
    9 posts
    Member since:
    Jul 2012

    Posted 03 Sep 2010 Link to this post

    Your tip showed me the way to implement it, I am sharing my solutions for others to use in case they face the same issue.

    I created a ConditionalNullStyleSelector class this way:

    public class ConditionalNullStyleSelector : StyleSelector
    {
      public enum CellColumn
      {
        HasDiscount,
        Price,
        InStock
      }
      
      public override Style SelectStyle(object item, DependencyObject container)
      {
        Product product;
        Style style = null;
      
        if(item is Product)
        {
          product = (Product)item;
            
          switch(ColumnToCheck)
          {
            case CellColumn.HasDiscount:
              style = PickStyle(product.HasDiscount);
              break;
            case CellColumn.Price:
              style = PickStyle(product.Cost);
              break;
            case CellColumn.InStock:
              style = PickStyle(product.InStock);
              break;
        default:
          style = PickStyle(0);
              break;
          }
        }
      
        return style;
      }
      
      public CellColumn ColumnToCheck { get; set; }
      
      private Style PickStyle(object value)
      {
        ResourceDictionary converterDictionary = new ResourceDictionary()
        {
          Source = new Uri("/SilverlightApplication1;component/Assets/Styles.xaml",
                             UriKind.RelativeOrAbsolute)
        };
      
        if (value == null)
        {
          return converterDictionary["NullCellRedStyle"] as Style;
        }
        else
        {
          return converterDictionary["NullCellWhiteStyle"] as Style;
        }
      }
    }

    In the Styles file

    <helpers:ConditionalNullStyleSelector x:Key="HasDiscountStyleSelector"
                                ColumnToCheck ="HasDiscount"/>
    <helpers:ConditionalNullStyleSelector x:Key="PriceStyleSelector"
                              ColumnToCheck ="Price"/>
    <helpers:ConditionalNullStyleSelector x:Key="InStockStyleSelector"
                              ColumnToCheck ="InStock"/>
      
      
    <Style x:Key="NullCellRedStyle" TargetType="telerik:GridViewCell">
      <Setter Property="Background" Value="#FFFFD8D8"/>
    </Style>
          
    <Style x:Key="NullCellWhiteStyle" TargetType="telerik:GridViewCell">
      <Setter Property="Background" Value="#FFFFFFFF"/>
    </Style>

    And In the GridView

    <telerik:GridViewDataColumn Header="Has Discount" 
                       DataMemberBinding="{Binding Path=HasDiscount}" 
                       CellStyleSelector="{StaticResource HasDiscountStyleSelector}" />
    <telerik:GridViewDataColumn Header="Price" 
                       DataMemberBinding="{Binding Path=Price}" 
                       DataFormatString="{} {0:C0}" 
                       CellStyleSelector="{StaticResource PriceStyleSelector}" />
    <telerik:GridViewDataColumn Header="InStock" 
                       DataMemberBinding="{Binding Path=InStock}" 
                       CellStyleSelector="{StaticResource InStockStyleSelector}" />

    Juan Mejia







  4. Ben
    Ben avatar
    1 posts
    Member since:
    Oct 2010

    Posted 14 Feb 2012 Link to this post


    private
    void grdWorkOrderItems_RowLoaded(object sender, RowLoadedEventArgs e)
          {
              if (e.Row is GridViewRow)
              {
                  foreach (GridViewCell cell in e.Row.Cells)
                  {
                      switch (cell.Column.UniqueName)
                      {
                          case "selectColumn":
                              break;
                          case "omrsColumn":
                              break;
                          default:
                              Binding cellStyleBinding = new Binding
                              {
                                  Source = e.Row.DataContext,
                                  Path = new PropertyPath("IsChecked"),
                                  Converter = LayoutRoot.Resources["CheckedStyleSelector"] as IValueConverter,
                              };
                              cell.SetBinding(GridViewCell.StyleProperty, cellStyleBinding);
                              break;
                      }
                  }
              }
          }

    For the sake of completeness, I was actually able to bind to the cell style and supply a converter as in the first example by setting the Binding source in the codebehind such as in the above code...I didn't; however, test it long enough to determine if there were any major issues, as that technique proved to be pointless after I got the CellStyleSelector working correctly...regards,

    Ben
Back to Top