我有一个项目,它会抛出一些数据绑定错误.一个例子是:
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')
我的问题是,是否有办法确定实际声明此绑定的位置(无论是在XAML中还是在代码中声明).
到目前为止我尝试过的:
HorizontalContentAlignment; 我发现只有一个并删除它但我仍然收到消息,这似乎表明那不是错误的...你知道有什么其他的技巧可以让WPF吐出一些更有用的信息,说明这个绑定的确切位置在哪里?
UPDATE
经过一点点搜索后,我很确定这是由某种风格应用于某种原因造成的MenuItem.但是,我仍然无法确定声明错误绑定的位置.
更新2
我发现了这个问题.然而问题仍然存在,因为发现问题主要是基于错误消息中的有限信息在黑暗中搜索.
事实证明,绑定是以一种样式声明的.风格不在我的应用程序中.这可能是默认的风格MenuItem.所以为了解决这个问题,我现在只需手动设置HorizontalContentAlignment全部MenuItems.错误的原因在某种程度上与操作顺序有关,因为这MenuItem是在代码中生成的.我将分别发布一个新问题.
因此,就目前而言,故事的寓意是我觉得需要有一个更好的机制来确定故障绑定的声明位置.我希望看到类似绑定的堆栈跟踪...
如果有人知道确定在代码或标记中声明绑定的位置的任何其他工具或方法,我会将问题保持打开一段时间.
我发布了另一个关于应用于MenuItems此处的样式/绑定的问题.
我创建了一个usercontrol,它有2个依赖项属性.我想将这些依赖项属性绑定到mainViewModel的属性,这样每当用户控件中的某些内容发生更改时,父属性的属性就会更新.
我尝试过,正常绑定但是没有用.如何将用户控件的DP绑定到父级的属性.
我试过这个:UC:
<TextBox Name="TextBox" Text="{Binding ElementName=UCName, Path=DP1, Mode=TwoWay}"/>
Run Code Online (Sandbox Code Playgroud)
主窗口:
<UCName:UCName Width="330" CredentialName="{Binding Path=DP1, Mode=TwoWay}"></UCName:UCName>
Run Code Online (Sandbox Code Playgroud)
谢谢
我试图解决一些经常发生的数据绑定错误,如下所示
System.Windows.Data Error: 40 : BindingExpression path error: 'Active' property not found on 'object' ''FrameViewModel' (HashCode=55649279)'. BindingExpression:Path=Active; DataItem='FrameViewModel' (HashCode=55649279); target element is 'ContentControl' (Name='HeaderBorder'); target property is 'NoTarget' (type 'Object')
Run Code Online (Sandbox Code Playgroud)
我有一个附加的依赖属性Active
public bool Active
{
get
{
return (bool)GetValue(ActiveProperty);
}
set
{
SetValue(ActiveProperty, value);
}
}
public static readonly DependencyProperty ActiveProperty
= DependencyProperty.RegisterAttached("Active", typeof(bool), typeof(Card));
Run Code Online (Sandbox Code Playgroud)
绑定在控件模板中的主题文件中设置
<ControlTemplate x:Key="FrameHeader" TargetType="ContentControl">
.....
.....
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding Path=Active}" Value="True">
<Setter TargetName="HeaderBackground" Property="Background" Value="{DynamicResource SelectedHeaderBrush}"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=Active}" Value="False">
<Setter TargetName="HeaderBackground" Property="Background" Value="{DynamicResource …Run Code Online (Sandbox Code Playgroud)