Binding can only be set in the DependencyProperty of a DependencyObject - when a property is overridden by a new one

I have a class hierarchy as follows, and the binding to the VisibleRange property is thrown into the constructor.

Given the class hierarchy:

// 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 the constructor (XAML) is here:

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

I get this exception:

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

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

+6
source share
2 answers

The "Dependency Property" is what you registered:

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

And when you look at this statement, you can see that it is being registered with typeof(IRange)

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

Yes, but it exposes the "normal" property, not the Dependency property.
Another factor is that properties have different types.

+10
source

Try writing in your initialization code of your XAxis , for example

AxisBase XAxis = new DateTimeAxis ()

Must work.

0
source

Source: https://habr.com/ru/post/912539/


All Articles