Here's the top of my 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:DialogMetad开发者_StackOverflow社区ata.Instance>
<Framework:DialogMetadata SizeToContent="WidthAndHeight"
ResizeMode="NoResize"
ConfirmCommand="{Binding ConfirmCommand, RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type UserControl}}}"
ConfirmButtonText="Run"/>
</Framework:DialogMetadata.Instance>
Long story short, I've got a UserControl, and I've defined an attached property: DialogMetadata.Instance
that takes an object of type DialogMetadata
. This construct is just a list of attached properties for me to use if I ever launch this control as a stand-alone dialog.
This all works for the most part, I've got it picking up the SizeToContent
, ResizeMode
, and ConfirmButtonText
.
However, In the spirit of MVVM I want to pass this command along to the dialog to be executed when we click the Confirm Button. So as you can see I've attempted to bind the Command off the ViewModel into the DialogMetadata's ConfirmCommand DependencyProperty.
The DP Is Defined like this:
#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
However when I bind up like in the first code block: (Note: The Property 'ConfirmCommand' Exists on the data-context of the UserControl, and has a non-null value.)
Binding
{Binding ConfirmCommand, RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type UserControl}}}
Error
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
{Binding ConfirmCommand, ElementName=Master}
Error
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')
Anyone have any insight into what's going on with my bindings?
"Cannot find source for binding"
I suspect that Framework:DialogMetadata's datacontext is not your ViewModel. You should check that and if its not you need to pass the datacontext of your UserControl to to DialogMetaData.
I must admit I don't correctly understand <Framework:DialogMetadata.Instance>
stuff so I am just trying to point out the symptoms rather than the disease. :D
精彩评论