Can't bind foreground color of cell programatically

2 posts, 0 answers
  1. Kateřina
    Kateřina avatar
    1 posts
    Member since:
    Nov 2014

    Posted 05 Jun 2015 Link to this post

    Hello,

    I need to create columns of RadGridView dynamically, so I have to style the cell in code. Here is my code:

    1.<telerik:RadGridView x:Name="ResultsGridView" Grid.Row="1" Grid.Column="0" Margin="10" ItemsSource="{Binding ResultsG}" AutoGenerateColumns="False" RowHeight="25" EnableColumnVirtualization="True" IsReadOnly="True"/>

    01.foreach (vstupni_zdroj inputSource in inputSources)
    02.{
    03.    GridViewDataColumn col = new GridViewDataColumn
    04.    {
    05.        Header = inputSource.name_short,
    06.        UniqueName = inputSource.name_short.ToLower()
    07.    };
    08. 
    09.    DataTemplate template = new DataTemplate();
    10.    var cell = new FrameworkElementFactory(typeof(Telerik.Windows.Controls.Label));
    11. 
    12.    Binding cellBindText = new Binding("InputSources[" + inputSource.id_vstupni_zdroj + "]");
    13.    cellBindText.Converter = new TextConverter();
    14.    cell.SetBinding(Telerik.Windows.Controls.Label.ContentProperty, cellBindText);
    15. 
    16.    Binding cellBindColor = new Binding("InputSources[" + inputSource.id_vstupni_zdroj + "]");
    17.    cellBindColor.Converter = new StringToBrushConverter();
    18.    cell.SetBinding(Telerik.Windows.Controls.Label.ForegroundProperty, cellBindColor);
    19. 
    20.    Binding cellBindColorB = new Binding("InputSources[" + inputSource.id_vstupni_zdroj + "]");
    21.    cellBindColor.Converter = new StringToBrushConverter();
    22.    cell.SetBinding(Telerik.Windows.Controls.Label.BackgroundProperty, cellBindColorB);
    23.                 
    24.    template.VisualTree = cell;
    25.    template.Seal();
    26.                 
    27.    col.CellTemplate = template;
    28.    ResultsGridView.Columns.Add(col);
    29.}

     

    The list named InputSources is Dictionary<uint, string> which contains ID and some color in string. StringToBrushConverter converts string to Brush using this code:

    01.SolidBrush brush;
    02.if (value is string)
    03.{
    04.    switch (value as string)
    05.    {
    06.        case "red":
    07.            brush = new SolidBrush(System.Drawing.Color.Red);
    08.            break;
    09.        case "green":
    10.            brush = new SolidBrush(System.Drawing.Color.Green);
    11.            break;
    12.        default:
    13.            brush = new SolidBrush(System.Drawing.Color.Black);
    14.            break;
    15.    }
    16.    return brush;
    17.}
    18.return value;

    My problem is that the foreground color of the label does not change, but background does, even if the approach is the same for both. So I'd like to know, what am I doing wrong.

    Thank you for any help.
    Kate

  2. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 10 Jun 2015 Link to this post

    Hi Kate,

    Setting Background and Foreground should work similarly. In order to apply conditional styling, I would suggest you applying a CellStyleSelector. You can also check the CellStyleSelector online demo. 

    Would you please try this approach instead? How does it work for you?

    Regards,
    Dimitrina
    Telerik
    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 Feedback Portal and vote to affect the priority of the items
Back to Top