This thread relates to http://www.telerik.com/community/forums/silverlight/gridview/images-disappear-when-scrolling-in-gridview.aspx
I am creating a custom data column type
I have created a few converter classes to help:
Where a grid is set up like so:
_____________________________________________________________________________________
MetricName1_Value| MetricName1_BackgroundColor| MetricName1_ForegroundColor | MetricName2_Value ...
Since the names of these metrics are dynamic, I have set the columns in code:
RFCellStyle is defined as:
but I am trying to have a dynamic conversion.
I've tried to use it in the XAML, but this would require nested binding statements in the ControlTemplate, not to mention I would need to know the Metric name ahead of time.
I've tried something like:
Of course, my converter receives a binding data type, which doesn't work.
I've also tried overriding CreateCellElement
Any thoughts would be more than welcome. Please let me know how I can assist.
I am creating a custom data column type
I have created a few converter classes to help:
using System; |
using System.Net; |
using System.Windows; |
using System.Windows.Controls; |
using System.Windows.Documents; |
using System.Windows.Ink; |
using System.Windows.Input; |
using System.Windows.Media; |
using System.Windows.Media.Animation; |
using System.Windows.Shapes; |
using System.Windows.Data; |
using System.Globalization; |
namespace ABC.SLApp1 |
{ |
public class TemplateNameBackgroundConverter : IValueConverter |
{ |
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) |
{ |
return (string)value + "_BackgroundColor"; |
} |
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) |
{ |
string stemp = (string)value; |
return stemp.Substring(0, stemp.IndexOf("_")); |
} |
} |
public class TemplateNameForegroundConverter : IValueConverter |
{ |
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) |
{ |
return (string)value + "_ForegroundColor"; |
} |
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) |
{ |
string stemp = (string)value; |
return stemp.Substring(0, stemp.IndexOf("_")); |
} |
} |
public class RFBackgroundConverter : IValueConverter |
{ |
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) |
{ |
string hexColor = (string)value; |
if (!string.IsNullOrEmpty(hexColor)) |
{ |
if (hexColor.Length == 7) |
return new SolidColorBrush(Colors.Green); |
return new SolidColorBrush(Colors.Yellow); |
} |
return new SolidColorBrush(Colors.White); |
} |
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) |
{ |
Color cTemp = ((SolidColorBrush)value).Color; |
return string.Format("#{0:x}{1:x}{2:x}{3:x}", cTemp.A, cTemp.R, cTemp.G, cTemp.B); |
} |
} |
public class RFForegroundConverter : IValueConverter |
{ |
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) |
{ |
string hexColor = (string)value; |
if (!string.IsNullOrEmpty(hexColor)) |
{ |
if (hexColor.Length == 7) |
return new SolidColorBrush(Colors.Gray; |
return new SolidColorBrush(Colors. |
} |
return new SolidColorBrush(Colors.Orange); |
} |
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) |
{ |
Color cTemp = ((SolidColorBrush)value).Color; |
return string.Format("#{0:x}{1:x}{2:x}{3:x}", cTemp.A, cTemp.R, cTemp.G, cTemp.B); |
} |
} |
} |
Where a grid is set up like so:
_____________________________________________________________________________________
MetricName1_Value| MetricName1_BackgroundColor| MetricName1_ForegroundColor | MetricName2_Value ...
Since the names of these metrics are dynamic, I have set the columns in code:
new CustomDataColumn |
{ |
MatricName = m.MetricName, |
UniqueName = "Matrix_" + m.MatricName, |
DataType = Type.GetType(m.DataType), |
IsFilterable = true, |
IsReadOnly = true, |
IsSortable = true, |
Header = textBlockHeader, |
HeaderCellStyle = Resources["HeaderCellStyle"] as Style, |
CellStyle = Resources["RFCellStyle"] as Style, |
DataMemberBinding = new Binding(m.MatricName + "_Value"), |
Width = 50, |
MinWidth = 30, |
TextAlignment = TextAlignment.Center |
}; |
RFCellStyle is defined as:
<Style x:Key="RFCellStyle" TargetType="grid:GridViewCell"> |
<Setter Property="Template" Value="{StaticResource RFCellTemplate}"/> |
</Style> |
but I am trying to have a dynamic conversion.
I've tried to use it in the XAML, but this would require nested binding statements in the ControlTemplate, not to mention I would need to know the Metric name ahead of time.
I've tried something like:
Style RFCellStyle = new Style("grid:GridViewCell"); |
RFBackgroundConverter rFBgConv = new RFBackgroundConverter(); |
RFCellStyle.Setters.SetValue(Control.BackgroundProperty, rFBgConv.Convert(new Binding(m.MatricName + "_BackgroundColor"), typeof(String), null, System.Globalization.CultureInfo.InvariantCulture)); |
RFCellStyle.Setters.SetValue(Control.ForegroundProperty, rFBgConv.Convert(new Binding(m.MatricName + "_ForegroundColor"), typeof(String), null, System.Globalization.CultureInfo.InvariantCulture)); |
var col = new CustomDataColumn |
{ |
MatricName = m.MatricName, |
UniqueName = "Matrix_" + m.MatricName, |
DataType = Type.GetType(m.DataType), |
IsFilterable = true, |
IsReadOnly = true, |
IsSortable = true, |
Header = textBlockHeader, |
HeaderCellStyle = Resources["HeaderCellStyle"] as Style, |
CellStyle = RFCellStyle, |
DataMemberBinding = new Binding(m.MatricName + "_Value"), |
Width = 50, |
MinWidth = 30, |
TextAlignment = TextAlignment.Center |
}; |
Of course, my converter receives a binding data type, which doesn't work.
I've also tried overriding CreateCellElement
public override FrameworkElement CreateCellElement(GridViewCell cell, object dataItem) in CustomDataColumn class,
but this too fails on virtualization (it leaves some blank columns formatted, as though createcellelement is not being called.)
var presenter = new TextBlock(); |
var opp = dataItem as DynamicObject; |
if (opp == null) return presenter; |
var metrics = opp.GetValue<OpportunityMetric>(MatricName); |
if (metrics == null) return presenter; |
presenter.SetBinding(TextBlock.TextProperty, new Binding |
{ |
Path = new PropertyPath(DataMemberBinding.Path.Path), |
Mode = BindingMode.OneWay |
}); |
if (!string.IsNullOrEmpty(metrics.BackgroundColor)) |
cell.SetValue(Control.BackgroundProperty, new SolidColorBrush(Colors.Gray)); |
else |
cell.SetValue(Control.BackgroundProperty, new SolidColorBrush(Colors.White)); |
Any thoughts would be more than welcome. Please let me know how I can assist.