0

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?

Community
  • 1
  • 1
Michał J. Gąsior
  • 1,457
  • 3
  • 21
  • 39
  • In order for anyone to provide you with a valid `Binding path`, we'd need to know where `rect4` is declared in relation to the `Menu`... if you'd actually like an answer, perhaps you could enlighten us? – Sheridan Oct 08 '14 at 11:05
  • I doubt that there is some bug in `CommandParameter` Binding, sometimes the parameter is not actually passed and what we have is always null. So I prefer to using `CommandBinding` or just don't use `CommandParameter` at all. – King King Oct 08 '14 at 11:12
  • I have edited the code. The `Rectangle` is in the same `StackPanel` as the `Menu`. Is it because the `MenuItem` is nested in `Menu` and that's why the name is not seen? How can I use `CommandBinding` to pass a reference to a control? – Michał J. Gąsior Oct 08 '14 at 11:50

0 Answers0