I have been trying to implement a "jump to" menu as described here. As long as I did a binding inside a simple Button, everything was OK:
<Button Foreground="Black" Style="{StaticResource MyButton}"
Command="{StaticResource JumpToElementCommand}"
CommandParameter="{Binding ElementName=rect4}"
Content="Go to rect4"/>
The problem showed up when I tried to use this method inside a MenuItem:
<StackPanel Orientation="Vertical">
<Menu x:Name="myJumpMenu" Style="{StaticResource MyMenu}"
Grid.Column="1">
<MenuItem Name="jumpToRectItem"
Command="{StaticResource JumpToElementCommand}"
Style="{StaticResource MyMenuItemStyle}">
<MenuItem.CommandParameter>
<Binding ElementName="rect4"/>
</MenuItem.CommandParameter>
</MenuItem>
</Menu>
<Rectangle Width="100" Height="700"
Fill="Purple" Margin="10" x:Name="rect4"/>
</StackPanel>
I have tried to cope with it with a solution described here by putting a NameScope static method in code behind of UserControl in various places (in a constructor and in Loaded and Initialized events).
NameScope.SetNameScope(contextMenu, NameScope.GetNameScope(this));
Instead of getting a reference to the Rectangle, I get a null value. Any idea how to make it work?