views:

212

answers:

2

Hi,

I want to be able to add a Binding to some properties of a DataGridTextColumn (i.e. Width, Sorting order, etc.), however it seems that those properties are not DependencyPropertys, so they can't be bound to. Another answer suggested subclassing DataGridTextColumn to expose those properties as DependencyPropertys, however I can't seem to find any info on how to do this.

Thanks, Robert

A: 

Try this:

public class BindableGridColumn : DataGridTextColumn
    {
        public DataGridLength BindableWidth
        {
            get { return Width; }
            set { 
                  SetValue(BindableWidthProperty, value);
                  Width = value; 
                }
        }

        // Using a DependencyProperty as the backing store for BindableWidth.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty BindableWidthProperty =
            DependencyProperty.Register("BindableWidth", typeof(DataGridLength), typeof(BindableGridColumn), new PropertyMetadata(DataGridLength.Auto));
    }
James Cadd
Er... that doesn't work since when it's set via the binding, no callback is called, and when set via the property, the DependancyProperty isn't changed.
Robert Fraser
Sorry, must have been too early for me to attempt that! Yes, I can see that setting BindableWidth, which you'd be binding to, wouldn't end up changing the DP. Added a call to SetValue in the property setter. The other way won't work, i.e. if you set the Width property directly the BindableWidth won't be updated, but I don't think that matters for this scenario.
James Cadd
Thanks! Doesn't matter anyway, though, because since DataGridColumn isn't a FrameworkElement, it can't be bound to.
Robert Fraser
A: 

In Silverlight, only subclasses of FrameworkElement (not DependencyObject) can have DependencyPropertys. So it's impossible to bind directly to a DataGridColumn's properties.

Robert Fraser