代码之家  ›  专栏  ›  技术社区  ›  Aren

非常奇怪的绑定问题:命令和自定义属性

  •  0
  • Aren  · 技术社区  · 15 年前

    下面是我的用户控件的顶部:

    <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>
    

    DialogMetadata.Instance 接受类型为 DialogMetadata

    这在很大程度上都是有效的,我已经把它捡起来了 SizeToContent ResizeMode ConfirmButtonText .

    多功能虚拟机 当我们单击确认按钮时,我想把这个命令传递给要执行的对话框。如您所见,我试图将ViewModel的命令绑定到DialogMetadata的 confirm命令 从属属性。

    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 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')


    1 回复  |  直到 15 年前
        1
  •  1
  •   NVM    15 年前

    “找不到 来源

    我怀疑框架:DialogMetadata的datacontext不是您的ViewModel。您应该检查它,如果不是,您需要将UserControl的datacontext传递给DialogMetaData。

    <Framework:DialogMetadata.Instance> 所以我只是想指出症状而不是疾病。:D个

    推荐文章