本文介绍了ElementName中的ElementName绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有其他人注意到,包含在 ContextMenu 对象中的 MenuItem 对象的ElementName绑定不能正确解析?查看此示例:

 < Window x:Class =EmptyWPF.Window1
xmlns =http: //schemas.microsoft.com/winfx/2006/xaml/presentation
xmlns:x =http://schemas.microsoft.com/winfx/2006/xaml
标题=Window1 Height =300Width =300
x:Name =window>
< Grid x:Name =gridBackground =Wheat>
< Grid.ContextMenu>
< ContextMenu x:Name =menu>
< MenuItem x:Name =menuItemHeader =WindowTag ={Binding ElementName = window}Click =MenuItem_Click/>
< MenuItem Header =GridTag ={Binding ElementName = grid}Click =MenuItem_Click/>
< MenuItem Header =MenuTag ={Binding ElementName = menu}Click =MenuItem_Click/>
< MenuItem Header =菜单项Tag ={Binding ElementName = menuItem}Click =MenuItem_Click/>
< / ContextMenu>
< /Grid.ContextMenu>
< Button Content =Menu
Horizo​​ntalAlignment =CenterVerticalAlignment =Center
Click =MenuItem_ClickTag ={Binding ElementName = menu}/>
< Menu Horizo​​ntalAlignment =CenterVerticalAlignment =Bottom>
< MenuItem x:Name =anotherMenuItemHeader =WindowTag ={Binding ElementName = window}Click =MenuItem_Click/>
< MenuItem Header =GridTag ={Binding ElementName = grid}Click =MenuItem_Click/>
< MenuItem Header =MenuTag ={Binding ElementName = menu}Click =MenuItem_Click/>
< MenuItem Header =菜单项Tag ={Binding ElementName = anotherMenuItem}Click =MenuItem_Click/>
< / Menu>
< / Grid>
< / Window>

除了ContextMenu中包含的绑定之外,所有绑定都很好用。他们在运行时将错误打印到输出窗口。



有任何工作知道吗?这里发生了什么?

解决方案

我发现一个更简单的解决方案。



在UserControl的代码中:

  NameScope.SetNameScope(contextMenu,NameScope.GetNameScope(this)); 


Has anybody else noticed that Bindings with ElementName do not resolve correctly for MenuItem objects that are contained within ContextMenu objects? Check out this sample:

<Window x:Class="EmptyWPF.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300"
    x:Name="window">
    <Grid x:Name="grid" Background="Wheat">
        <Grid.ContextMenu>
            <ContextMenu x:Name="menu">
                <MenuItem x:Name="menuItem" Header="Window" Tag="{Binding ElementName=window}" Click="MenuItem_Click"/>
                <MenuItem Header="Grid" Tag="{Binding ElementName=grid}" Click="MenuItem_Click"/>
                <MenuItem Header="Menu" Tag="{Binding ElementName=menu}" Click="MenuItem_Click"/>
                <MenuItem Header="Menu Item" Tag="{Binding ElementName=menuItem}" Click="MenuItem_Click"/>
            </ContextMenu>
        </Grid.ContextMenu>
        <Button Content="Menu"
                HorizontalAlignment="Center" VerticalAlignment="Center"
                Click="MenuItem_Click" Tag="{Binding ElementName=menu}"/>
        <Menu HorizontalAlignment="Center" VerticalAlignment="Bottom">
            <MenuItem x:Name="anotherMenuItem" Header="Window" Tag="{Binding ElementName=window}" Click="MenuItem_Click"/>
            <MenuItem Header="Grid" Tag="{Binding ElementName=grid}" Click="MenuItem_Click"/>
            <MenuItem Header="Menu" Tag="{Binding ElementName=menu}" Click="MenuItem_Click"/>
            <MenuItem Header="Menu Item" Tag="{Binding ElementName=anotherMenuItem}" Click="MenuItem_Click"/>
        </Menu>
    </Grid>
</Window>

All of the bindings work great except for the bindings contained within the ContextMenu. They print an error to the Output window during runtime.

Any one know of any work arounds? What's going on here?

解决方案

I found a much simpler solution.

In the code behind for the UserControl:

NameScope.SetNameScope(contextMenu, NameScope.GetNameScope(this));

这篇关于ElementName中的ElementName绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 05:34