【问题标题】:Very Strange Binding issue: Commands & Custom Properties非常奇怪的绑定问题:命令和自定义属性
【发布时间】:2010-08-19 22:07:05
【问题描述】:

这是我的用户控件的顶部:

<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 的对象。如果我将此控件作为独立对话框启动时,此构造只是一个附加属性列表供我使用。

这在大多数情况下都有效,我已经找到了 SizeToContentResizeModeConfirmButtonText

但是,本着 MVVM 的精神,我想将此命令传递给对话框,以便在我们单击确认按钮时执行。如您所见,我尝试将 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 的数据上下文中,并且具有非空值。)

绑定

{Binding ConfirmCommand, RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type UserControl}}}

错误

System.Windows.Data 错误:4:无法通过引用“RelativeSource FindAncestor,AncestorType='System.Windows.Controls.UserControl',AncestorLevel='1'”找到绑定源。绑定表达式:路径=确认命令;数据项=空;目标元素是 'DialogMetadata' (Name='');目标属性是“ConfirmCommand”(输入“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')


有人知道我的绑定发生了什么吗?

【问题讨论】:

    标签: .net wpf data-binding


    【解决方案1】:

    “找不到来源进行绑定”

    我怀疑 Framework:DialogMetadata 的 datacontext 不是您的 ViewModel。您应该检查一下,如果不是,您需要将 UserControl 的数据上下文传递给 DialogMetaData。

    我必须承认我没有正确理解&lt;Framework:DialogMetadata.Instance&gt; 的东西,所以我只是想指出症状而不是疾病。 :D

    【讨论】:

    • 嗯,也许你的权利,即便如此,第一个绑定指向一个应该定位 UserControl 的相对源。它找不到要绑定到它的 DataContext 的用户控件。 Framework:DialogMetadata.Instance 只是一个附加属性,它类似于`Framework:DialogMetadata.Instance="{something here}"`,但由于我需要指定一个对象,所以它被拉出到一个标签中
    • 啊……所以它不完全是视图中的视图。在那种情况下,您确定 DialogMetaData 的父级确实是 UserControl?它只是在附加属性中定义,它不是 UserControl 的子项。
    • DataContext={Binding ElementName=Master, Path=DataContext}
    • ElementName 不起作用。我设法让它通过一种“黑客”工作。我在 DialogMetadata 上创建了一个名为 ParentElement 的新 DP,并在设置 AttachedProperty 时分配它。然后我使用了绑定:{Binding RelativeSource={RelativeSource Self}, Path=ParentElement.DataContext.ConfirmCommand} 这有点小技巧,但现在可以使用。如果您能想出一种简化它的方法,我将不胜感激。
    • DataContext={Binding ElementName=Master, Path=DataContext} 是否将其添加到 不起作用?我怀疑它应该。也会干净很多。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-03
    • 2014-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-13
    相关资源
    最近更新 更多