This is a migrated thread and some comments may be shown as answers.

Can't bind foreground color of cell programatically

1 Answer 424 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Kateřina
Top achievements
Rank 1
Kateřina asked on 05 Jun 2015, 12:38 PM

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

1 Answer, 1 is accepted

Sort by
0
Dimitrina
Telerik team
answered on 10 Jun 2015, 08:24 AM
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
Tags
GridView
Asked by
Kateřina
Top achievements
Rank 1
Answers by
Dimitrina
Telerik team
Share this question
or