WPF - Freezable in a style of a button not inheriting DataContext

admin

Administrator
Staff member
I am modeling an attached command pattern after the <a href="http://marlongrech.wordpress.com/2008/12/13/attachedcommandbehavior-v2-aka-acb/" rel="nofollow">AttachedCommandBehavior library here</a>. My button looks like this:

Code:
&lt;Button&gt;
    &lt;Button.Style&gt;
        &lt;Style TargetType="{x:Type Button}"&gt;
            &lt;Setter Property="vms:Attached.Behaviors"&gt;
                &lt;Setter.Value&gt;
                    &lt;vms:Behaviors&gt;
                        &lt;vms:Behavior Event="Click" 
                                      Command="{Binding ClickCommand}" /&gt;
                    &lt;/vms:Behaviors&gt;
                &lt;/Setter.Value&gt;
            &lt;/Setter&gt;
        &lt;/Style&gt;
    &lt;/Button.Style&gt;
&lt;/Button&gt;

Everything works great, but when the setter on the
Code:
Behavior
is executed, the Command is
Code:
null
.

Behavior is a
Code:
Freezable
, and Behaviors is a
Code:
FreezableCollection&lt;Behavior&gt;
. It just doesn't seem to be inheriting the DataContext from the Button.

On the other hand, this works correctly:

Code:
&lt;Button&gt;
    &lt;vms:Attached.Behaviors&gt;
        &lt;vms:Behavior Event="Click" Command="{Binding ClickCommand}" /&gt;
    &lt;/vms:Attached.Behaviors&gt;
&lt;/Button&gt;

Unfortunately I can't do it this way, because I need to target generated
Code:
ListViewItem
s using
Code:
ItemContainerStyle
.

Is there some way to get the DataContext in the Style?