gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 01:04:24 24 4
gpt4 key购买 nike

我有以下 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com