这是我的usercontrol的顶部:
<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>
长话短说,我有一个UserControl,我已经定义了一个附加属性:DialogMetadata.Instance
,它接受类型为DialogMetadata
的对象。如果我将此控件作为独立对话框启动,则此构造只是我要使用的附加属性列表。
这一切大部分都有用,我已经拿到了SizeToContent
,ResizeMode
和ConfirmButtonText
。
然而,本着 MVVM 的精神,我想将此命令传递给单击确认按钮时要执行的对话框。因此,您可以看到我尝试将View从ViewModel绑定到DialogMetadata的 ConfirmCommand DependencyProperty。
DP定义如下:
#region DP - ConfirmCommand
public static DependencyProperty ConfirmCommandProperty =
DependencyProperty.Register("ConfirmCommand", typeof (IBaseCommand), typeof (DialogMetadata),
new PropertyMetadata(null));
/// <summary>
/// The Confirmation Command for a dialog. This allows us to sync up to the user-control for
/// determining if we can can hit the Ok Button, and to perform additional logic
/// </summary>
public virtual IBaseCommand ConfirmCommand
{
get { return GetValue(ConfirmCommandProperty) as IBaseCommand; }
set { SetValue(ConfirmCommandProperty, value); }
}
#endregion
但是当我在第一个代码块中绑定时:(注意:属性'ConfirmCommand'存在于UserControl的数据上下文中,并且具有非空值。) < / p>
装订
{Binding ConfirmCommand, RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type UserControl}}}
错误
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')
装订
{Binding ConfirmCommand, ElementName=Master}
错误
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=Master'. BindingExpression:Path=ConfirmCommand; DataItem=null; target element is 'DialogMetadata' (Name=''); target property is 'ConfirmCommand' (type 'IBaseCommand')
任何人都对我的绑定有什么了解吗?
答案 0 :(得分:1)
“无法找到来源进行绑定”
我怀疑Framework:DialogMetadata的datacontext不是你的ViewModel。你应该检查一下,如果不是你需要将UserControl的datacontext传递给DialogMetaData。
我必须承认我没有正确理解<Framework:DialogMetadata.Instance>
的东西所以我只是想指出症状而不是疾病。 :d