I have a usercontrol done by myself that has a dependency property that is a collection:
private static readonly DependencyPropertyKey VerticalLinesPropertyKey = DependencyProperty.RegisterReadOnly("VerticalLines", typeof(VerticalLineCollection), typeof(DailyChart), new FrameworkPropertyMetadata(new VerticalLineCollection()));
public static DependencyProperty VerticalLinesProperty = VerticalLinesPropertyKey.DependencyProperty;
public VerticalLineCollection VerticalLines
{
get
{
return (VerticalLineCollection)base.GetValue(VerticalLinesProperty);
}
set
{
base.SetValue(VerticalLinesProperty, value);
}
}
I was filling this collection from XAML directly when a Window was using the control with code like:
<chart:DailyChart.VerticalLines>
<VerticalLine ... ... ... />
</chart:DailyChart.VerticalLines>
Now, I removed this fixed initialization from XAML and I want to bind the collection to a property of the ViewModel but I get the error:
Error 1 'VerticalLines' property cannot be data-bound.
Parameter name: dp
Any ideas?
In your XAML example, the parser sees that the VerticalLineCollection
type implements IList
and hence for each specified VerticalLine
will create a VerticalLine
object and then call Add
on the collection itself.
However, when you attempt to bind the collection, the semantics become "assign a new collection to the VerticalLines
property", which can't be done since this is a read-only dependency property. The setter on your property really should be marked as private, and in doing so you will get a compile-time error instead.
Hope this helps!