问题
I have this DependencyProperty
public ObservableCollection<DataTemplate> WizardTemplateCollection
{
get { return (ObservableCollection<DataTemplate>)GetValue(WizardTemplateCollectionProperty); }
set { SetValue(WizardTemplateCollectionProperty, value); }
}
// Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
public static readonly DependencyProperty WizardTemplateCollectionProperty =
DependencyProperty.Register("WizardTemplateCollection", typeof(ObservableCollection<DataTemplate>), typeof(CustomWizardControl), new PropertyMetadata(new ObservableCollection<DataTemplate>()));
And want to do this:
<custom:CustomWizardControl>
<custom:CustomWizardControl.WizardTemplateCollection>
<DataTemplate>
<Rectangle></Rectangle>
</DataTemplate>
<DataTemplate>
<Rectangle></Rectangle>
</DataTemplate>
<DataTemplate>
<Rectangle></Rectangle>
</DataTemplate>
</custom:CustomWizardControl.WizardTemplateCollection>
</custom:CustomWizardControl>
What DataType do i need? Or how can i initialize a ObservableCollection in XAML.
Additional:
public class CustomWizardControl : Control {}
回答1:
Your CustomWizardControl class must inherit from DepenedencyObject or one of its derived types like for example UIElement or Control:
public class CustomWizardControl : Control
{
public ObservableCollection<DataTemplate> WizardTemplateCollection
{
get { return (ObservableCollection<DataTemplate>)GetValue(WizardTemplateCollectionProperty); }
set { SetValue(WizardTemplateCollectionProperty, value); }
}
...
}
This works:
public class CustomWizardControl : Control
{
public CustomWizardControl()
{
WizardTemplateCollection = new ObservableCollection<DataTemplate>();
}
public ObservableCollection<DataTemplate> WizardTemplateCollection
{
get { return (ObservableCollection<DataTemplate>)GetValue(WizardTemplateCollectionProperty); }
set { SetValue(WizardTemplateCollectionProperty, value); }
}
public static readonly DependencyProperty WizardTemplateCollectionProperty =
DependencyProperty.Register("WizardTemplateCollection", typeof(ObservableCollection<DataTemplate>), typeof(CustomWizardControl), new PropertyMetadata(null));
}
<local:CustomWizardControl x:Name="ctrl">
<local:CustomWizardControl.WizardTemplateCollection>
<DataTemplate>
<Rectangle></Rectangle>
</DataTemplate>
<DataTemplate>
<Rectangle></Rectangle>
</DataTemplate>
<DataTemplate>
<Rectangle></Rectangle>
</DataTemplate>
</local:CustomWizardControl.WizardTemplateCollection>
</local:CustomWizardControl>
<TextBlock Text="{Binding WizardTemplateCollection.Count, ElementName=ctrl}" />
回答2:
You cannot set the generic parameter of ObservableCollection<T> directly in XAML.
Instead you should create your custom DataTemplateCollection inherited from ObservableCollection<DataTemplate>. Then you will be able to use your collection as usual.
public class DataTemplateCollection : ObservableCollection<DataTemplate>
{
}
<custom:CustomWizardControl>
<custom:CustomWizardControl.WizardTemplateCollection>
<custom:DataTemplateCollection>
<DataTemplate>
<Rectangle></Rectangle>
</DataTemplate>
<DataTemplate>
<Rectangle></Rectangle>
</DataTemplate>
<DataTemplate>
<Rectangle></Rectangle>
</DataTemplate>
</custom:DataTemplateCollection>
</custom:CustomWizardControl.WizardTemplateCollection>
</custom:CustomWizardControl>
Additional note: NEVER initialize the default value of dependency properties with mutable objects, because this single mutable instance will be used by every control instance. Instead you must set the default value to null and assing the initial value in the constructor.
来源:https://stackoverflow.com/questions/43068894/initialize-collection-of-datatemplates-in-xaml