I am trying to extend vbfox's code discussed in
Using CommandBindings in MVVM that allows databinding in a custom CommandBindings class.
Instead of having to do:
I'd like to be able to do use XAML's implicit collection support to do:
The problem is that Mvvm.CommandBindings is an attached property of type MvvmCommandBindingCollection. MvvmCommandBindingCollection is currently not an actual Collection butcontains an embedded collection of type FreezableCollection<MvvmCommandBinding>. XAML implicit collection support only works if the ContentProperty attribute is set to something thatis a collection not just contains one.
Looking at Writing Collection Properties it says:
"If your property is a dependency property, then you may need to initialize the collection property as part of the default type constructor. This is because a dependency property takes its default value from metadata, and you typically do not want the initial value of a collection property to be a static, shared collection."
and Collection-Type Dependency Properties goes on to show an example:
But... that only works for a dependency property not an attached property. With an attached property you need to have a Target to attach to. And I don't see how in my constructor to initialize the attached property without knowing who the eventual target is going to be.
My question is: Is it even possible to have a Collection-Type Attached Property (as opposed to a Collection-Type Dependency Property which all the examples show)?
Instead of having to do:
<f:Mvvm.CommandBindings><f:MvvmCommandBindingCollection><f:MvvmCommandBinding Command="f:MyRoutedCommands.SomeRoutedCommand" Target="{Binding MyCommandInViewModel}" CanExecuteChangedSuggestRequery="True" /></f:MvvmCommandBindingCollection></f:Mvvm.CommandBindings>
I'd like to be able to do use XAML's implicit collection support to do:
<f:Mvvm.CommandBindings><f:MvvmCommandBinding Command="f:MyRoutedCommands.SomeRoutedCommand" Target="{Binding MyCommandInViewModel}" CanExecuteChangedSuggestRequery="True" /></f:Mvvm.CommandBindings>
The problem is that Mvvm.CommandBindings is an attached property of type MvvmCommandBindingCollection. MvvmCommandBindingCollection is currently not an actual Collection butcontains an embedded collection of type FreezableCollection<MvvmCommandBinding>. XAML implicit collection support only works if the ContentProperty attribute is set to something thatis a collection not just contains one.
Looking at Writing Collection Properties it says:
"If your property is a dependency property, then you may need to initialize the collection property as part of the default type constructor. This is because a dependency property takes its default value from metadata, and you typically do not want the initial value of a collection property to be a static, shared collection."
and Collection-Type Dependency Properties goes on to show an example:
public Aquarium() : base() { SetValue(AquariumContentsPropertyKey, new List()); }
But... that only works for a dependency property not an attached property. With an attached property you need to have a Target to attach to. And I don't see how in my constructor to initialize the attached property without knowing who the eventual target is going to be.
My question is: Is it even possible to have a Collection-Type Attached Property (as opposed to a Collection-Type Dependency Property which all the examples show)?