diff --git a/Stylet/Xaml/CommandAction.cs b/Stylet/Xaml/CommandAction.cs
index 4f3d5b7..bf040d6 100644
--- a/Stylet/Xaml/CommandAction.cs
+++ b/Stylet/Xaml/CommandAction.cs
@@ -72,7 +72,14 @@ namespace Stylet.Xaml
{
var newTarget = View.GetActionTarget(this.Subject);
MethodInfo targetMethodInfo = null;
-
+
+ // If it's being set to the initial value, ignore it
+ // At this point, we're executing the View's InitializeComponent method, and the ActionTarget hasn't yet been assigned
+ // If they've opted to throw if the target is null, then this will cause that exception.
+ // We'll just wait until the ActionTarget is assigned, and we're called again
+ if (newTarget == View.InitialActionTarget)
+ return;
+
this.guardPropertyGetter = null;
if (newTarget == null)
{
diff --git a/Stylet/Xaml/EventAction.cs b/Stylet/Xaml/EventAction.cs
index 67e29e2..022db58 100644
--- a/Stylet/Xaml/EventAction.cs
+++ b/Stylet/Xaml/EventAction.cs
@@ -67,6 +67,13 @@ namespace Stylet.Xaml
var newTarget = View.GetActionTarget(this.subject);
MethodInfo targetMethodInfo = null;
+ // If it's being set to the initial value, ignore it
+ // At this point, we're executing the View's InitializeComponent method, and the ActionTarget hasn't yet been assigned
+ // If they've opted to throw if the target is null, then this will cause that exception.
+ // We'll just wait until the ActionTarget is assigned, and we're called again
+ if (newTarget == View.InitialActionTarget)
+ return;
+
if (newTarget == null)
{
if (this.targetNullBehaviour == ActionUnavailableBehaviour.Throw)
diff --git a/Stylet/Xaml/View.cs b/Stylet/Xaml/View.cs
index e3f9a62..96b82e4 100644
--- a/Stylet/Xaml/View.cs
+++ b/Stylet/Xaml/View.cs
@@ -10,6 +10,12 @@ namespace Stylet.Xaml
///
public class View : DependencyObject
{
+ ///
+ /// Initial value of the ActionTarget property.
+ /// This can be used as a marker - if the property has this value, it hasn't yet been assigned to anything else.
+ ///
+ public static readonly object InitialActionTarget = new object();
+
private static readonly ContentPropertyAttribute defaultContentProperty = new ContentPropertyAttribute("Content");
///
@@ -41,7 +47,7 @@ namespace Stylet.Xaml
/// The object's ActionTarget. This is used to determine what object to call Actions on by the ActionExtension markup extension.
///
public static readonly DependencyProperty ActionTargetProperty =
- DependencyProperty.RegisterAttached("ActionTarget", typeof(object), typeof(View), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.Inherits));
+ DependencyProperty.RegisterAttached("ActionTarget", typeof(object), typeof(View), new FrameworkPropertyMetadata(InitialActionTarget, FrameworkPropertyMetadataOptions.Inherits));
///
/// Fetch the ViewModel currently associated with a given object