WPF MenuItem.Command 绑定(bind)到 ElementName 结果为 System.Windows.Data 错误 : 4 : Cannot find source for binding with reference

标签 wpf data-binding xaml

我有以下 XAML:

<UserControl x:Class="EMS.Controls.Dictionary.TOCControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:vm="clr-namespace:EMS.Controls.Dictionary.Models"
    xmlns:diagnostics="clr-namespace:System.Diagnostics;assembly=WindowsBase"
    x:Name="root"  >

    <TreeView                    
        x:Name="TOCTreeView"                    
        Background="White"
         Padding="3,5"      
        ContextMenuOpening="TOCTreeView_ContextMenuOpening"
        ItemsSource="{Binding Children}" BorderBrush="{x:Null}"  >

        <TreeView.ItemTemplate>
            <HierarchicalDataTemplate ItemsSource="{Binding Path=Children, Mode=OneTime}">
                <Grid >
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"/>
                        <!--<ColumnDefinition Width="Auto"/>-->                       
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
                    <!--<CheckBox VerticalAlignment="Center" IsChecked="{Binding IsVisible}"/>-->   
                    <ContentPresenter Grid.Column="0" Height="16" Width="20"
                                    Content="{Binding LayerRepresentation}"  />
                    <!--<ContentPresenter Grid.Column="1"      >
                        <ContentPresenter.Content>
                            Test
                        </ContentPresenter.Content>
                    </ContentPresenter>-->
                    <TextBlock Grid.Column="2" FontWeight="Normal" Text="{Binding Path=Alias, Mode=OneWay}" >
                        <ToolTipService.ToolTip>
                            <TextBlock Text="{Binding Description}" TextWrapping="Wrap"/>
                        </ToolTipService.ToolTip>
                    </TextBlock>
                </Grid>
                <HierarchicalDataTemplate.ItemTemplate>
                    <HierarchicalDataTemplate ItemsSource="{Binding Path=Children, Mode=OneTime}">
                        <!--<DataTemplate>-->
                        <Grid >
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto"/>
                                <ColumnDefinition Width="Auto"/>                               
                                <ColumnDefinition Width="*"/>
                            </Grid.ColumnDefinitions>
                            <CheckBox VerticalAlignment="Center" IsChecked="{Binding IsVisible}"/>
                            <ContentPresenter Grid.Column="1"
                                    Content="{Binding LayerRepresentation, Mode=OneWay}"  />
                            <TextBlock Margin="0,1,0,1" Text="{Binding Path=Alias, Mode=OneWay}" Grid.Column="2">
                                <ToolTipService.ToolTip>
                                    <TextBlock Text="{Binding Description}" TextWrapping="Wrap"/>
                                </ToolTipService.ToolTip>                   
                            </TextBlock>
                        </Grid>
                        <!--</DataTemplate>-->
                    </HierarchicalDataTemplate>                  
                </HierarchicalDataTemplate.ItemTemplate>
            </HierarchicalDataTemplate>
        </TreeView.ItemTemplate> 

        <TreeView.ContextMenu>
            <ContextMenu>
                <MenuItem Name="miRemove" Header="Remove"
                          Command="{Binding ElementName=root, Path=RemoveItemCmd, 
                    diagnostics:PresentationTraceSources.TraceLevel=High}">
                    <MenuItem.Icon>
                        <Image Source="../images/16x16/Delete.png"/>
                    </MenuItem.Icon>
                </MenuItem>
                <MenuItem Header="Properties"
                          Command="{Binding ElementName=root, Path=GetItemPropertiesCmd}"/>               
            </ContextMenu>       
        </TreeView.ContextMenu>


    </TreeView>

</UserControl>

此 UserControl 的代码隐藏有两个 ICommand 属性,其名称为:RemoveItemCmd 和 GetItemPropertiesCmd。但是,我得到了

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=root'. BindingExpression:Path=RemoveItemCmd; DataItem=null; target element is 'MenuItem' (Name='miRemove'); target property is 'Command' (type 'ICommand')
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=root'. BindingExpression:Path=GetItemPropertiesCmd; DataItem=null; target element is 'MenuItem' (Name=''); target property is 'Command' (type 'ICommand')

当构造 UserControl 时。这是为什么?我该如何解决?

最佳答案

您无法使用上下文菜单中的元素名称进行绑定(bind)。上下文菜单与其放置目标之间的链接已断开。不过,您可以使用一些技巧来解决它......

  1. 将 RoutedUICommands 与 UserControl 上的命令绑定(bind)结合使用,则无需绑定(bind)。
  2. 使用上下文菜单的 DataContext 上的放置目标绑定(bind)。这允许您至少获取上下文菜单中显示的元素的数据上下文。

    DataContext="{BindingrelativeSource={RelativeSourceMode=Self},Path=PlacementTarget.DataContext}"

  3. (我认为这就是您想要的)您可以访问静态资源,ElementSpy允许您使用静态资源链接到窗口,以便您可以使用事实上的 ElementName 绑定(bind)。

关于WPF MenuItem.Command 绑定(bind)到 ElementName 结果为 System.Windows.Data 错误 : 4 : Cannot find source for binding with reference,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2617122/

相关文章:

wpf - MVVM 将 viewmodel 属性子项绑定(bind)到 viewmodel 属性

c# - 使用自定义属性 XAML WPF 动态设置内容属性

wpf - 如何转义 WPF 绑定(bind)路径中的斜杠字符,或者如何解决?

c# - 转换器不能应用于需要类型 System.Windows.Data.IValueConverter 的属性

javascript - 如何使用 Polymer 1.0 dom-bind 公开局部变量

c# - LEAP Motion C# XAML WPF 应用程序

c# - WPF 与 XNA 渲染数千个 Sprite

WPF : Move and resize window at once time

c# - 序列化 View 或 View 模型

c# - WPF中的轻量级处理动画资源?