views:

391

answers:

3

I am attempting to create an AttachedProperty for a DataGridColumn within Silverlight 3.0 and I am having some issues.

Here is the AttachedProperty:

public class DataGridColumnHelper
{
    public static readonly DependencyProperty HeaderProperty =
        DependencyProperty.RegisterAttached("Header", typeof(string), typeof(DataGridColumnHelper),
                                            new PropertyMetadata(OnHeaderPropertyChanged));

    private static void OnHeaderPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        string header = GetHeader(d);

        var dataGridColumn = d as DataGridColumn;

        if (dataGridColumn == null)
        {
            return;
        }

        dataGridColumn.Header = GetHeader(dataGridColumn);
    }

    public static string GetHeader(DependencyObject obj)
    {
        return (string)obj.GetValue(HeaderProperty);
    }

    public static void SetHeader(DependencyObject obj, string value)
    {
        obj.SetValue(HeaderProperty, value);
    }

}

As you can see it is really simple, I am trying to overcome the limitation that the Header Property in the DataGridColumn class cannot be bound.

This XAML works as expected...

<Controls:DataGridTextColumn Binding="{Binding OwnerName}"                                                                         
                         HeaderStyle="{StaticResource DataGridColumnHeaderStyle}"

                         Behaviors:DataGridColumnHelper.Header="User Name"/>

However this XAML throws an error...(Specifically: {System.Windows.Markup.XamlParseException: AG_E_PARSER_PROPERTY_NOT_FOUND [Line: 224 Position: 112] at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator) ....})

<Controls:DataGridTextColumn Binding="{Binding OwnerName}"                                                                         
                         HeaderStyle="{StaticResource DataGridColumnHeaderStyle}"

                         Behaviors:DataGridColumnHelper.Header="{Binding Resources.UserNameListViewHeading, Source={StaticResource Labels}}"/>

Just for experimentation I attached this property (with the binding syntax above) to a DataGrid and checked the DataGridColumnHelper.Header property in the OnHeaderPropertyChanged method and the value was correct (and an exception wasn't thrown)

It is my understanding that the object that the AttachedProperty is attached to must be a DependencyProperty. Looking through Reflector, DataGridColumn (from which DataGridTextColumn derives) derives from DependencyProperty.

Can somebody please shed some light on this? I am trying to Localize our application, and I am having trouble with the DataGrid. I am sure I can do this in code-behind, but I am trying to avoid that.

A: 

Try using this, im assuming UserName is a property in your viewmodel

    <Controls:DataGridTextColumn Binding="{Binding OwnerName}"                                                                         
HeaderStyle="{StaticResource DataGridColumnHeaderStyle}"
Behaviors:DataGridColumnHelper.Header="{Binding UserName}"/>

I cant test your scenario so my post is just an idea. Might work, might not.

Neil
The Binding I am attempting to use is from a Resource File, because we are localizing our strings. The binding of a DataGridTextColumn is based off the Items source (assuming we don't specify a source as I did in the above example), therefore the DataContext is not the same as the DataGrid. Thanks
Chris Mancini
A: 

Hello,

I'm actually very interested in any solution. I'm facing the same problem and haven't benn able to find any kind of solution...

Hope someone will be able to help...

Cheers,

Geros

Geros
+1  A: 

Chris, the problem is very simple, this won't work because the DataGridTextColumn is "detached" from the Visual Tree. Your DataGridTextColumn object is rooted in the Columns collection of the DataGrid - see the indirection. So even attached properties will not work as you expect. Now there is a way to make all this work using something I'm calling Attached Bindings, see:

http://www.orktane.com/Blog/post/2009/09/29/Introducing-nRouteToolkit-for-Silverlight-%28Part-I%29.aspx

Just remember to attach the binding properties using something that is in the VisualTree (so the Grid holding the column would do just fine.)

Hope this helps.

Rishi
I have been following your posts on the nRoute Toolkit, and am looking forward to experimenting with it. Thanks for the descriptive answer.
Chris Mancini
What about for Run in Silverlight? Attached property with binding on Run doesn't work in SIlverlight...
Michael Sync