Closed Sally-Xu closed 9 years ago
Instead of using ElementName=XXX, using RelativeSource = {RelativeSource ItemsControlParent} is working.
This one works:
<DataTemplate>
<TextBlock MouseLeftButtonDown="{EventBinding Command={Binding Path=DataContext.MenuClickCmd, RelativeSource={RelativeSource ItemsControlParent}}, CommandParameter={Binding}}}" Text="{Binding Header}" />
</DateTemplate>
This doesn't work in Silverlight either.
If I remember correctly, that was how it works in Silverlight (by using ElementName). I don't think Silverlight/WPF even has ItemsControlParent in RelativeSource. But I like ItemsControlParent, just need to be documented it if that's how it supposed to work.
I tried the example in Silverlight and it doesn't work. Binding cannot find the specified ElementName because it belongs to a different logical tree. To be able to do this would require attaching the ContextMenu to the parent logical tree; however, this introduces other problems.
Just tried to set RelativeSource with AncestorType=ItemsControl
RelativeSource={RelativeSource AncestorType=ItemsControl} => This doesn't work either.
The only way to make it work is to set RelativeSource={RelativeSource ItemsControlParent}
Fayde requires Mode=FindAncestor
to be explicitly set.
Opened new issue #139 to track.
I'm trying to set binding in ContextMenu's ItemTemplate. Here is the testing XAML:
<TextBlock Grid.Row="1" MouseLeftButtonDown="{EventBinding Command={Binding Path=DataContext.MenuClickCmd, ElementName=Root, CommandParameter={Binding}}}" Text="Test" />
The TextBlock outside of the ContextMenu's ItemTemplate works fine. But the one inside the ItemTemplate can't find the "Root" Element set in the ElementName binding field. I'm getting "[EVENTBINDING]: Could not find command target for event 'MouseLeftButtonDown'." error at EventBindingExpression.ts:80 .