0

I am modeling an attached command pattern after the AttachedCommandBehavior library here. My button looks like this:

<Button>
    <Button.Style>
        <Style TargetType="{x:Type Button}">
            <Setter Property="vms:Attached.Behaviors">
                <Setter.Value>
                    <vms:Behaviors>
                        <vms:Behavior Event="Click" 
                                      Command="{Binding ClickCommand}" />
                    </vms:Behaviors>
                </Setter.Value>
            </Setter>
        </Style>
    </Button.Style>
</Button>

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

Behavior is a Freezable, and Behaviors is a FreezableCollection<Behavior>. It just doesn't seem to be inheriting the DataContext from the Button.

On the other hand, this works correctly:

<Button>
    <vms:Attached.Behaviors>
        <vms:Behavior Event="Click" Command="{Binding ClickCommand}" />
    </vms:Attached.Behaviors>
</Button>

Unfortunately I can't do it this way, because I need to target generated ListViewItems using ItemContainerStyle.

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

Snea
  • 1,867
  • 2
  • 14
  • 21
  • your link isn't valid. Can you edit and fix, please? – Robaticus Feb 05 '11 at 21:58
  • I'm not really following how your `vms:Attached.Behaviors` maps to that library. I can only find stuff like `CommandBehaviorCollection.Behaviors` and so on so I'm unable reproduce – Fredrik Hedblad Feb 05 '11 at 23:30
  • It is based on that library, but not the same. vm:Attached.Behaviors is a Behaviors (maps to CommandBehaviorCollection.Behaviors), but it is not a read-only dependency property. – Snea Feb 05 '11 at 23:57

2 Answers2

1

The Attached Command Behavior library is the germ of the idea that became Blend Behaviors. The Blend Behaviors are much more powerful and standardized and so I recommend you switch to using them. But whether you are using Attached Command Behavior or Blend Behaviors, the problem is essential the same: they don't work as expected when trying to set them using a style. I've solved this problem for Blend Behaviors with full support for binding in this StackOverflow answer:

Without testing it, I guess you have to move the ACB behavior to a resource marked with x:Shared="False" in order to get the binding to work.

Community
  • 1
  • 1
Rick Sladkey
  • 33,988
  • 6
  • 71
  • 95
  • This looks very promising, I'll take a look at it. – Snea Feb 06 '11 at 00:29
  • This worked for the button example above, but unfortunately still did not work for a ListView's `ItemContainerStyle`. Any idea how it could work for that? – Snea Feb 06 '11 at 06:04
  • In an items control the binding will be evaluated in context of the item, not its parent so you either have to have your command in the items context or use relative source or element name binding to get to a different context that has one. – Rick Sladkey Feb 06 '11 at 07:06
  • Actually the command for the item _was_ null, and everything seems to be working well now. – Snea Feb 06 '11 at 07:41
0

I had the same problem, and using RelativeSource did the trick. I'll show you my before and after code...

Before: (This DIDN'T work)

<DataTemplate x:Key="MenuNodeWithChildrenTemplate">
    <StackPanel Orientation="Horizontal"
            behaviors:EventCommand.CommandToRun="{Binding OpenMenuItem}"
            behaviors:EventCommand.EventName="MouseLeftButtonUp">
        <Label Content="{Binding Title}"/>
        <Label Content="{Binding Description}"/>
    </StackPanel>
</DataTemplate>

After: (This DOES work)

<DataTemplate x:Key="MenuNodeWithChildrenTemplate">
    <StackPanel Orientation="Horizontal"
            behaviors:EventCommand.CommandToRun="{Binding Path=DataContext.OpenMenuItem, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ItemsControl}}}"
            behaviors:EventCommand.EventName="MouseLeftButtonUp">
        <Label Content="{Binding Title}"/>
        <Label Content="{Binding Description}"/>
    </StackPanel>
</DataTemplate>

You'll obviously have to tweak the parameters of the Relative Source to your specific situation. It seems that, for whatever reason, attached properties don't inherit the data context, so you have to tell if how to.

Quanta
  • 465
  • 5
  • 14