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 GridViewDataColumn04. {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