2010-08-19 38 views
0

這裏是我的用戶的頂部:非常奇怪的綁定問題:命令和自定義屬性

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

但是,本着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」存在於用戶控件的數據上下文,以及具有非空值。)

綁定

{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

「找不到結合」

我懷疑框架:DialogMetadata的DataContext的是不是你的視圖模型。你應該檢查它,如果不是,你需要將你的UserControl的datacontext傳遞給DialogMetaData。

我必須承認我沒有正確理解<Framework:DialogMetadata.Instance>東西,所以我只是想指出症狀而不是疾病。 :D

+0

Hrm,也許你的權利,即使如此,第一個綁定點指向一個相對的源應該找到UserControl。它無法找到usercontrol綁定到它的DataContext。 框架:DialogMetadata.Instance只是一個附加的屬性,它類似於'Framework:DialogMetadata.Instance =「{something here}」'但由於我需要指定一個對象,它被拉出到一個Tag – Aren 2010-08-19 22:38:09

+0

Aaah .. 。所以它不完全是一種觀點。在這種情況下,你確定DialogMetaData的父代確實是UserControl?它只是在一個附屬屬性中定義的,它不是UserControl的子節點。 – NVM 2010-08-19 22:58:30

+0

DataContext = {Binding ElementName = Master,Path = DataContext} – NVM 2010-08-19 23:05:50