.net 非常奇怪的绑定问题:Commands&;自定义属性
下面是我的用户控件的顶部:.net 非常奇怪的绑定问题:Commands&;自定义属性,.net,wpf,data-binding,.net,Wpf,Data Binding,下面是我的用户控件的顶部: <UserControl x:Class="MyApp.Common.Controls.Views.SimpleView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="h
<UserControl x:Class="MyApp.Common.Controls.Views.SimpleView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:Framework="http://www.memoryexpress.com/UIFramework"
mc:Ignorable="d"
Height="130" Width="450"
x:Name="Master"
>
<!-- Behaviour under the context of the generic dialog -->
<Framework:DialogMetadata.Instance>
<Framework:DialogMetadata SizeToContent="WidthAndHeight"
ResizeMode="NoResize"
ConfirmCommand="{Binding ConfirmCommand, RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type UserControl}}}"
ConfirmButtonText="Run"/>
</Framework:DialogMetadata.Instance>
但是,当我像在第一个代码块中那样绑定时:(注意:属性'ConfirmCommand'存在于UserControl的数据上下文中,并且具有非null值。)
绑定
{Binding ConfirmCommand, RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type UserControl}}}
{Binding ConfirmCommand, ElementName=Master}
错误
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.UserControl', AncestorLevel='1''. BindingExpression:Path=ConfirmCommand; DataItem=null; target element is 'DialogMetadata' (Name=''); target property is 'ConfirmCommand' (type 'IBaseCommand')
错误
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.UserControl', AncestorLevel='1''. BindingExpression:Path=ConfirmCommand; DataItem=null; target element is 'DialogMetadata' (Name=''); target property is 'ConfirmCommand' (type 'IBaseCommand')
System.Windows.Data错误:4:找不到引用为“ElementName=Master”的绑定源。BindingExpression:Path=ConfirmCommand;DataItem=null;目标元素是“DialogMetadata”(名称=“”);目标属性为“ConfirmCommand”(类型为“IBaseCommand”)
有人对我的绑定有什么见解吗?“找不到绑定的源代码” 我怀疑Framework:DialogMetadata的datacontext不是您的ViewModel。您应该对此进行检查,如果不是,则需要将UserControl的datacontext传递给DialogMetaData
我必须承认我没有正确理解
的内容,所以我只是想指出症状,而不是疾病DHrm,也许你是对的,尽管如此,第一个绑定指向一个相对源,它应该定位UserControl。它找不到要绑定到其DataContext的usercontrol。Framework:DialogMetadata.Instance只是一个附加属性,它类似于`Framework:DialogMetadata.Instance=“{something here}”`但是由于我需要指定一个对象,所以它被拉到了一个TagAaah中。。。因此,这并不完全是一个视图中的视图。在这种情况下,您确定DialogMetaData的父级确实是UserControl吗??它只是在附加属性中定义的,而不是UserControl的子级。DataContext={Binding ElementName=Master,Path=DataContext}ElementName
不起作用。我设法通过一种“黑客”方式使它工作起来。我在DialogMetadata上创建了一个名为ParentElement
的新DP,并在设置AttachedProperty时分配它。然后我使用了binding:{binding RelativeSource={RelativeSource Self},Path=ParentElement.DataContext.ConfirmCommand}
这有点像黑客,但目前还可以使用。如果您能想出一种简化它的方法,我将不胜感激?我想应该是这样。它也会更干净。