在附加 WPF 附加行为之前触发 PropertyChangedCallback
PropertyChangedCallback fired before WPF attached behavior gets attached
我正在使用自定义 WPF 行为(来自 System.Windows.Interactivity 的行为)显示几个依赖属性,其中一个是字符串。该行为还覆盖 OnAttached
以获取对其 AssociatedObject
UI 控件的引用。
当附加的 属性 数据绑定到 viewModel 并且稍后在某个时候更改(并通知)时,一切似乎都很好:OnAttached 已被解雇 "at the beginning",然后 PropertyChangedCallback 被触发。
我看到的问题是 属性 未绑定,但在 XAML 中设置为 "static" 值。在这种情况下,PropertyChangedCallback 在 OnAttached 之前被触发,此时行为还不知道其关联的 UI 控件并且基本上无法执行任何操作作为对 属性 变化的反应。
我想我遗漏了一些关于在这种情况下应该如何做的事情。理解这一点的任何帮助表示赞赏。 TA
编辑
如果在这种情况下可能有帮助,请在此处显示一些代码:
public class SomeUIControlBehaviour : Behavior<SomeUIControl>
{
protected override void OnAttached()
{
base.OnAttached();
_attachedUIControl = this.AssociatedObject;
}
protected override void OnDetaching()
{
base.OnDetaching();
_attachedUIControl = null;
}
private SomeUIControl _attachedUIControl;
private void MessageChanged()
{
if (_attachedUIControl != null)
{
// do something on it
}
else
{
// bummer!
}
}
// Text property + dependency property
public string Message
{
get { return (string)GetValue(MessageProperty); }
set { SetValue(MessageProperty, value); }
}
private static string _defaultMessage = String.Empty;
// Using a DependencyProperty as the backing store for Message. This enables animation, styling, binding, etc...
public static readonly DependencyProperty MessageProperty =
DependencyProperty.Register("Message",
typeof(string), typeof(SomeUIControlBehaviour),
new PropertyMetadata(_defaultMessage, MessagePropertyChanged));
private static void MessagePropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs evt)
{
//Debug.WriteLine("MessagePropertyChanged, on " + sender.GetType().Name + ", to value " + evt.NewValue);
SomeUIControlBehaviour behaviour = sender as SomeUIControlBehaviour;
if (behaviour == null)
{
Debug.Fail("Message property should be used only with SomeUIControlBehaviour");
return;
}
behaviour.MessageChanged();
}
}
根据评论,一个简单的答案可能是:
当行为被附加时,只需检查 属性 是否已经有一个值(可能与默认值不同),在这种情况下执行 PropertyChangedCallback
应该执行的操作。
我正在使用自定义 WPF 行为(来自 System.Windows.Interactivity 的行为)显示几个依赖属性,其中一个是字符串。该行为还覆盖 OnAttached
以获取对其 AssociatedObject
UI 控件的引用。
当附加的 属性 数据绑定到 viewModel 并且稍后在某个时候更改(并通知)时,一切似乎都很好:OnAttached 已被解雇 "at the beginning",然后 PropertyChangedCallback 被触发。
我看到的问题是 属性 未绑定,但在 XAML 中设置为 "static" 值。在这种情况下,PropertyChangedCallback 在 OnAttached 之前被触发,此时行为还不知道其关联的 UI 控件并且基本上无法执行任何操作作为对 属性 变化的反应。
我想我遗漏了一些关于在这种情况下应该如何做的事情。理解这一点的任何帮助表示赞赏。 TA
编辑
如果在这种情况下可能有帮助,请在此处显示一些代码:
public class SomeUIControlBehaviour : Behavior<SomeUIControl>
{
protected override void OnAttached()
{
base.OnAttached();
_attachedUIControl = this.AssociatedObject;
}
protected override void OnDetaching()
{
base.OnDetaching();
_attachedUIControl = null;
}
private SomeUIControl _attachedUIControl;
private void MessageChanged()
{
if (_attachedUIControl != null)
{
// do something on it
}
else
{
// bummer!
}
}
// Text property + dependency property
public string Message
{
get { return (string)GetValue(MessageProperty); }
set { SetValue(MessageProperty, value); }
}
private static string _defaultMessage = String.Empty;
// Using a DependencyProperty as the backing store for Message. This enables animation, styling, binding, etc...
public static readonly DependencyProperty MessageProperty =
DependencyProperty.Register("Message",
typeof(string), typeof(SomeUIControlBehaviour),
new PropertyMetadata(_defaultMessage, MessagePropertyChanged));
private static void MessagePropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs evt)
{
//Debug.WriteLine("MessagePropertyChanged, on " + sender.GetType().Name + ", to value " + evt.NewValue);
SomeUIControlBehaviour behaviour = sender as SomeUIControlBehaviour;
if (behaviour == null)
{
Debug.Fail("Message property should be used only with SomeUIControlBehaviour");
return;
}
behaviour.MessageChanged();
}
}
根据评论,一个简单的答案可能是:
当行为被附加时,只需检查 属性 是否已经有一个值(可能与默认值不同),在这种情况下执行 PropertyChangedCallback
应该执行的操作。