我一直在尝试按照here所述实现“跳转到”菜单。只要我在一个简单的Button中进行绑定,一切都很好:
<Button Foreground="Black" Style="{StaticResource MyButton}"
Command="{StaticResource JumpToElementCommand}"
CommandParameter="{Binding ElementName=rect4}"
Content="Go to rect4"/>
当我尝试在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>
我试图用一个描述here的解决方案来处理它,方法是将NameScope静态方法放在UserControl的代码后面的各个地方(在构造函数和Loaded和Initialized事件中)。
NameScope.SetNameScope(contextMenu, NameScope.GetNameScope(this));
我没有得到对Rectangle的引用,而是得到一个null值。知道如何让它发挥作用吗?