Bindings on DataGridPartEditBoxColumns

Editors for WPF Forum

Posted 13 years ago by Phil Devaney - Senior Software Engineer, Serck Controls Ltd
Version: 11.1.0545
Platform: .NET 4.0
Environment: Windows 7 (32-bit)
Avatar
I've been trying to set bindings on properties on PartEditBoxes used in data grid columns. Normally, the way to do this is to use the EditingElementStyle property, but because of the way DataGridBoundColumnBase.ApplyStandardValues works this doesn't work. The problem is that all the values from the DataGridPartEditBoxColumn are passed through to the PartEditBox, so these local values override any binding set in the EditingElementStyle.

To work around this, I made the following change to DataGridBoundColumnBase.ApplyValue and built my own version of Editors.Interop.DataGrid:

protected virtual void ApplyValue(DependencyProperty sourceProperty, FrameworkElement targetElement, DependencyProperty targetProperty) 
{
    var sourceValue = this.ReadLocalValue( sourceProperty );
    if ( sourceValue != DependencyProperty.UnsetValue )
    {
        var expr = sourceValue as BindingExpression;
        if ( expr != null )
            BindingOperations.SetBinding( targetElement, targetProperty, expr.ParentBinding );
        else
            targetElement.SetValue( targetProperty, this.GetValue( sourceProperty ) );
    }
}
This does two things:
- If there is no local value set on the column, the property is not set on the edit box so it will pick up any style value. This may have an adverse effect if a property has different defaults for the column and the edit box.
- If there is a binding set on the column, it sets the same binding on the edit box. This seems to work OK in the cases I tried, but there may be subtle problems related to the DataContext.

Can you think of a better way of doing this? If not, should this fix be added to the project on CodePlex?

Phil

Comments (1)

Posted 13 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Phil,

We are mimicing how the native DataGrid columns work, in regards to passing the value down. Basically, setting a property on the column is effectively the same as setting it on the underlying editor. We did make a change to ApplyValue in August that has not been release yet. This change will not set the value on the underlying control, if it has not been set on the column. This is similar to what you do, but we will also clear the value:
protected virtual void ApplyValue(DependencyProperty sourceProperty, FrameworkElement targetElement, DependencyProperty targetProperty) {
    // 8/22/2011 - If the property is not set on the column, then do not pass down to the element (10F-15E35C2E-480E)
    if (DependencyPropertyHelper.GetValueSource(this, sourceProperty).BaseValueSource == BaseValueSource.Default)
        targetElement.ClearValue(targetProperty);
    else
        targetElement.SetValue(targetProperty, this.GetValue(sourceProperty));
}
As for the binding part, passing the binding like you do won't work in several cases (i.e. ElementName, DataContext). Our columns should be passing any changes to it's columns down to the underlying editor. There is a method commented out in DataGridBoundColumnBase called RefreshCellContent. If you uncomment this, then changes to the column properties will get passed to the underlying editor. I'm not sure why this was commented out, but it has been so since it was first coded.

So if you uncomment this method, you will then be able to bind the columns properties and changes will be passed down. With the change to ApplyValue above, you can set bind in the EditingElementStyle assuming you don't set the associated property on the column.

Both these changes will be included in the next maintenance release.


Actipro Software Support

The latest build of this product (v24.1.2) was released 7 days ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.