1

My goal is to format the background color of a cell in a RadGridView in my WPF project to be that of the value in the underlying data. This data is dynamic however so I cannot use pre-defined templates

There are RGB values in my underlying table (albeit with spaces instead of commas so I use a converter to fix that) and in a test project where I had a single "colour" column and template, everything worked a treat:

    <Window.Resources>

    <local:StringToBrushConverter x:Key="lStringToBrushConverter"></local:StringToBrushConverter>

    <DataTemplate x:Key="ColourCellTemplate">
        <Border Name="lBorder" BorderThickness="3" Margin="1" CornerRadius="1" Width="100"
                BorderBrush="{Binding Colour, Converter={StaticResource lStringToBrushConverter}}">
            <TextBlock Background="{Binding ElementName=lBorder, Path=BorderBrush}" ></TextBlock> 
        </Border>
    </DataTemplate>

</Window.Resources>

The problem is now how I can work with this template to be usable in a dynamic scenario. Essentially all I need to change is "Colour" in this part:

BorderBrush="{Binding Colour, Converter={StaticResource lStringToBrushConverter}}"

but I am not sure how that can be achieved in code-behind. I can use TryFindResource to get hold of the DataTemplate but the next step to change the Binding is where I'm stuck.

I have looked at this example from Telerik which works in different circumstances when the value is being compared against another value but it doesn't solve my problem.

Many thanks..

miriyo
  • 103
  • 1
  • 11
  • So why CellTemplateSelector does not help? It allows to select template based on data item, which seems exactly what you need. – Evk Apr 05 '16 at 15:40
  • Well I could have hundreds of rows all with a different value and I don't want to create a template for each eventuality. That would work if I had e.g. just red, blue, green or something but the RGB values are selected by the user using a colour picker. – miriyo Apr 06 '16 at 07:22
  • Your problem is not clear then. You have RGB values in each row, represented by Colour property. Why you cannot just bind to Colour as you describe, why change binding? Or you mean all the difference is that you have 3 columns (R, G and B) instead of one? – Evk Apr 06 '16 at 07:24
  • Because my data is loaded dynamically and the column name in the underlying data is not always "Colour". It could be anything. I have a property in my DataColumn class that tells me the column contains an RGB field but that's all I know at design time. – miriyo Apr 06 '16 at 07:47
  • So in underlying data there is a column which contains the name of another column, which in turn contains color, right? – Evk Apr 06 '16 at 08:26
  • No. I have an Explorer style application with a TreeView on the left. When I click on a node, data is loaded into a grid on the right side of the Window. The columns for the grid are different for each node and in the event AutoGeneratingColumn for my grid, I find out (a) what data type the data is and (b) what the name of the field is. Columns for Node 1: TextColumn, TextColumn, RGB Column, ImageColumn Columns for Node 2: TextColumn, RGB Column, TextColumn, TextColumn. RGB Column. (Rare that two RGB Columns are present, but I have to cater for it) ...and so on. – miriyo Apr 06 '16 at 09:25

1 Answers1

0

If I understand your problem correctly from comments, you can just bind to the whole object (not to some specific property, like Colour) with a converter, and in converter inspect it's properties:

public class ColorColumnsConverter : IValueConverter {
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
        if (value == null || value == DependencyProperty.UnsetValue)
            return null;
        var folder = value as Folder; // some of your data types
        if (folder != null) {
            return folder.ColorProperty;
        }
        var jpegFile = value as JpegFile; // some another of your data types
        if (jpegFile != null) {
            return jpegFile.Colour;
        }
        // etc
        return null;            
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
        throw new NotImplementedException();
    }
}
Evk
  • 98,527
  • 8
  • 141
  • 191
  • Hi, I have edited my template to just bind to the entire DataRow and when I inspect the value in my "RGB to Colour converter" when debugging I see the row in question. Again I don't have pre-defined tables so I need to be able to tell the template or converter the column name in question at some point. – miriyo Apr 06 '16 at 09:58
  • Ah, you are using DataRow, thats kind of unusual nowadays. Well then just inspect columns of this DataRow.Table.Columns, find those you need, then extract values from your DataRow. – Evk Apr 06 '16 at 10:06
  • I still have the problem that I don't know which column is using the Converter. I certainly have the DataRow object as the value but need a way to pass the field name via the parameter – miriyo Apr 06 '16 at 17:50
  • Where do you get this field name in the first place? I thought you grab all information from DataRow itself. – Evk Apr 06 '16 at 17:52