If I create a custom control as follows:
public class MyControl : ContentControl
{
public static readonly DependencyProperty ItemsProperty =
DependencyProperty.Register(
"Items",
typeof(ObservableCollection<object>),
typeof(MyControl),
new PropertyMetadata(null));
public MyControl()
{
Items = new ObservableCollection<object>();
}
public ObservableCollection<object> Items
{
get { return (ObservableCollection<object>)GetValue(ItemsProperty); }
set { SetValue(ItemsProperty, value); }
}
}
And then allow the user to contact him in Xaml as follows:
<DataTemplate>
<MyControl Items="{Binding ItemsOnViewModel}"/>
</DataTemplate>
Then the binding never works! This is due to the Dependency Property Priority , which sets the CLR Set values above the template bindings!
So, I understand why this is not working, but I wonder if there is a solution. Is it possible to provide a default ItemProperty value for the new ObservableCollection for lazy MyControl consumers who just want to add items programmatically, allowing MVVM Power Control My Control users to communicate with the same property through a DataTemplate?
Silverlight WPF. DynamicResource , Silverlight: (
:
, SetCurrentValue(ItemsProperty, new ObservableCollection<object>()); , - WPF. , . - Silverlight? , !: S
:
-, SetCurrentValue .NET3.5 , Silverlight . , () .
SetCurrentValue .NET3.5 Value Coercion
Value Coercion Silverlight