tags:

views:

82

answers:

1

I'm trying to extend the datagridcolumn a bit so I can make column widths percentage based, rather then absolute in silverlight. This way no matter what size the grid, the columns take up a specified percentage of the grid.

Anyway, this is my first step

public static class DataGridColumnBehaviors
{
    public static readonly DependencyProperty WidthPercentageProperty =
        DependencyProperty.RegisterAttached("WidthPercentage", typeof(double?), typeof(DataGridColumnBehaviors),
            new PropertyMetadata(null, OnWidthPercentagePropertyChanged));

    public static double? GetWidthPercentage(DependencyObject d)
    {
        return (double?)d.GetValue(WidthPercentageProperty);
    }

    public static void SetWidthPercentage(DependencyObject d, double? value)
    {
        d.SetValue(WidthPercentageProperty, value);
    }

    public static void OnWidthPercentagePropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
    {

    }
}

And in the XAML I'm doing

            <data:DataGridTemplateColumn MinWidth="200" 
                                         dataBehaviors:DataGridColumnBehaviors.WidthPercentage="5.0"
                                         Header="Name">
                <data:DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Name}" />
                    </DataTemplate>
                </data:DataGridTemplateColumn.CellTemplate>
                <data:DataGridTemplateColumn.CellEditingTemplate>
                    <DataTemplate>
                        <TextBox Text="{Binding Name, Mode=TwoWay}" />
                    </DataTemplate>
                </data:DataGridTemplateColumn.CellEditingTemplate>
            </data:DataGridTemplateColumn>

This is producing the following message at runtime

AG_E_PARSER_BAD_PROPERTY_VALUE [Line: 85 Position: 100]

Line 85 is this:

dataBehaviors:DataGridColumnBehaviors.WidthPercentage="5.0

Any ideas?

+1  A: 

You can't convert from a double to a double? at the CLR level like that. And you almost certainly don't want to.

Silverlight uses doubles and then uses double.NaN and double.PositiveInfinity to represent 'special' values.

Alun Harford
Ah, so setting the default value to double.Nan rather the using double?... that did the trick thank you!
Jeff
I'd probably pick double.NaN but you're probably going to have to check for this 'special' value in your attached behaviour and do something special (like not setting the width or whatever) so you can pick any special value you like.
Alun Harford