I have created custom control with generic collection dependency property. But whenever this property is changed from xaml, value in view model in setter is null.
CustomControl:
public IList A
{
get { return (IList)GetValue(AProperty); }
set { SetValue(AProperty, value); }
}
public static readonly DependencyProperty AProperty =
DependencyProperty.Register(nameof(A), typeof(IList), typeof(CustomControl), new PropertyMetadata(new List<object>()));
ViewModel:
List<B> collectionB;
public List<B> CollectionB
{
get { return collectionB; }
set
{
if (collectionB == value) return;
collectionB = value;
}
}
If I change type of CollectionB to List<object> it works fine.
A="{Binding CollectionB, Mode=TwoWay}"
? And how does the control change the collection? – ClemensList<object>
. That can't be assigned to a source property of typeList<B>
. OneWayToSource is not a well-suited approach here. Do not set a default value at all and use a TwoWay Binding instead. – ClemensIList
property to aList<A>
. If the property is stillnull
you are doing something wrong when you set it, or maybe you don't set it all. It's hard to say without some sample code. – mm8