Binding can only be set on a DependencyProperty of a DependencyObject - when property is overridden with new

Go To StackoverFlow.com

6

I have a class hierarchy as follows and binding to the VisibleRange property is throwing in the designer.

Given the class hierarchy here:

// Base class
public abstract class AxisBase : ContentControl, IAxis
{
    public static readonly DependencyProperty VisibleRangeProperty = DependencyProperty.Register(
        "VisibleRange", typeof(IRange), typeof(AxisBase), 
         new PropertyMetadata(default(IRange), OnVisibleRangeChanged));

    public IRange VisibleRange
    {
        get { return (IRange)GetValue(VisibleRangeProperty); }
        set { SetValue(VisibleRangeProperty, value); }
    }
}

// Derived class
public class DateTimeAxis : AxisBase
{
        public new IRange<DateTime> VisibleRange
        {
            get { return (IRange<DateTime>)GetValue(VisibleRangeProperty); }
            set { SetValue(VisibleRangeProperty, value); }
        }
}

// And interface definitions
public interface IRange<T> : IRange 
{
}

And designer (XAML) here:

<local:DateTimeAxis Style="{StaticResource XAxisStyle}"                                               
       VisibleRange="{Binding ElementName=priceChart, 
                      Path=XAxis.VisibleRange, Mode=TwoWay}"/>

I get this exception:

A 'Binding' cannot be set on the 'VisibleRange' property of type 'DateTimeAxis'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject.

The derived class DateTimeAxis is exposing the VisibleRange property which is overridden by the new keyword. I can't add a generic typeparam to the base AxisBase class, and I also need to access the property in both classes. So, I'm wondering given these constraints, if anyone has any suggestions on how to do this better to avoid the designer exceptions?

2012-04-05 21:31
by Dr. ABT
Andrew Burnett-Thom: did you try to code like in mine answer. Did it work - Tigran 2012-04-05 21:58


10

The 'dependency property' is the thing you registered with:

 public static readonly DependencyProperty VisibleRangeProperty = 
    DependencyProperty.Register("VisibleRange", typeof(IRange), typeof(AxisBase), ...);

And when you look at that statement you can see that it is registering with typeof(IRange)

The derived class DateTimeAxis is exposing the VisibleRange property which is overridden by the new keyword.

Yes, but it is exposing a 'normal' property, not a Dependency Property.
Another factor is that the properties have different types.

2012-04-05 21:36
by Henk Holterman
It's exposing the DependencyProperty of the base - I suppose this is where its going wrong huh? I might can the new override and just put casts throughout the cod - Dr. ABT 2012-04-05 21:41
Yes, DPs are strange beasts. I would circumvent this - Henk Holterman 2012-04-05 21:45
Done, and works :) Thanks for the tip - Dr. ABT 2012-04-05 21:47


0

Try to write in your code initialization of your XAxis, like

AxisBase XAxis = new DateTimeAxis ()

Should work.

2012-04-05 21:36
by Tigran
I'm curious if it does.. - Henk Holterman 2012-04-05 21:47
@HenkHolterman: actually me too, will write to OP.. : - Tigran 2012-04-05 21:58
Not sure what you mean by this - the DateTimeAxis is constructed in Xaml, not C# cod - Dr. ABT 2012-04-05 22:01
What I mean is that actual type of the object could be of a base type but real one of the child. In this case binding could be, may be, obscured with a fact that dependency property is not present in real object type, but in its based type istead. Was just a guess and I was just curious if it worked. If you didn't try don't bother, I will try it when have a time - Tigran 2012-04-05 22:04
Ads