【问题标题】:ContextMenu style produces weird Binding errors without using any BindingContextMenu 样式在不使用任何绑定的情况下产生奇怪的绑定错误
【发布时间】:2015-08-10 21:21:23
【问题描述】:

我有一个很奇怪的问题:

我使用这种 XAML 样式:

<Style TargetType="ContextMenu">
    <Setter Property="ItemsSource">
        <Setter.Value>
            <CompositeCollection>
                <MenuItem Header="Test"/>
            </CompositeCollection>
        </Setter.Value>
     </Setter>
</Style>

它可以工作,但我的控制台出现以下错误:

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.ItemsControl', AncestorLevel='1''. BindingExpression:Path=HorizontalContentAlignment; DataItem=null; target element is 'MenuItem' (Name=''); target property is 'HorizontalContentAlignment' (type 'HorizontalAlignment')
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.ItemsControl', AncestorLevel='1''. BindingExpression:Path=VerticalContentAlignment; DataItem=null; target element is 'MenuItem' (Name=''); target property is 'VerticalContentAlignment' (type 'VerticalAlignment')
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.ItemsControl', AncestorLevel='1''. BindingExpression:Path=HorizontalContentAlignment; DataItem=null; target element is 'MenuItem' (Name=''); target property is 'HorizontalContentAlignment' (type 'HorizontalAlignment')
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.ItemsControl', AncestorLevel='1''. BindingExpression:Path=VerticalContentAlignment; DataItem=null; target element is 'MenuItem' (Name=''); target property is 'VerticalContentAlignment' (type 'VerticalAlignment')
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.ItemsControl', AncestorLevel='1''. BindingExpression:Path=HorizontalContentAlignment; DataItem=null; target element is 'MenuItem' (Name=''); target property is 'HorizontalContentAlignment' (type 'HorizontalAlignment')
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.ItemsControl', AncestorLevel='1''. BindingExpression:Path=VerticalContentAlignment; DataItem=null; target element is 'MenuItem' (Name=''); target property is 'VerticalContentAlignment' (type 'VerticalAlignment')
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.ItemsControl', AncestorLevel='1''. BindingExpression:Path=HorizontalContentAlignment; DataItem=null; target element is 'MenuItem' (Name=''); target property is 'HorizontalContentAlignment' (type 'HorizontalAlignment')
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.ItemsControl', AncestorLevel='1''. BindingExpression:Path=VerticalContentAlignment; DataItem=null; target element is 'MenuItem' (Name=''); target property is 'VerticalContentAlignment' (type 'VerticalAlignment')

谁能解释一下是什么导致了错误以及如何摆脱它们?

【问题讨论】:

  • 你能创建一个小的独立样本并分享。因为这是一个通用的上下文菜单,它可以应用于任何东西。问题可能与您的 xaml 中的任何其他控件有关

标签: c# wpf


【解决方案1】:

这是 WPF 中的一个已知问题,MenuItem 的默认样式尝试绑定到 ItemsControl 的 Horizo​​ntalContentAlignment 和 VerticalContentAlignment。

您应该能够通过覆盖这两个属性来绕过这些错误:

   <Style TargetType="MenuItem">
    <Setter Property="HorizontalContentAlignment" Value="Left"/>
    <Setter Property="VerticalContentAlignment" Value="Center"/>
   </Style>

这些帖子中有关此问题的更多信息:

ListBox with Grid as ItemsPanelTemplate produces weird binding errors

MenuItem added programmatically causes Binding error

【讨论】:

  • 谢谢!这也可能是在某个操作后立即关闭 ContextMenu 的原因吗? (在此操作之前,它的效果非常好!)
猜你喜欢
  • 2010-09-14
  • 2013-07-01
  • 2018-08-17
  • 2014-12-31
  • 2012-11-30
  • 1970-01-01
  • 2015-08-18
  • 1970-01-01
相关资源
最近更新 更多