Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

How to implement a Collection-Type Attached Property instead of a Collection-Type Dependency Property?

$
0
0
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:

<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)?


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>