开发者

DependencyObject.AssociatedObject is always null

开发者 https://www.devze.com 2023-02-22 03:46 出处:网络
I am trying to write a customer behavior to set some column widths to 0 if my \"Visibility\" (which is just a boo开发者_运维百科l in this case) property is false... My problem is that when my on chang

I am trying to write a customer behavior to set some column widths to 0 if my "Visibility" (which is just a boo开发者_运维百科l in this case) property is false... My problem is that when my on changed event fires it my AssociatedObject is always null.

Here is the relevant sample code, mybe someone can see where I am going wrong.

  public static readonly DependencyProperty VisibilityProperty =
        DependencyProperty.Register("Visibility", typeof(bool), typeof(HideRadGridViewColumnBehavior), 
        new PropertyMetadata(OnVisibilityPropertyChanged));

private static void OnVisibilityPropertyChanged(DependencyObject target, DependencyPropertyChangedEventArgs args)
{
  if (((HideRadGridViewColumnBehavior)target).AssociatedObject == null)
    MessageBox.Show("AssociatedObject is null");
}

Thanks for any help...


how are you attaching the behavior? and can you show some code of the behavior?

the AssociatedObject is set either after the call to Attach or through listing the behavior within <i:Interaction.Behaviors></i:Interaction.Behaviors>


Thanks Markus Hütter for idea. This is how I have implemented in PasswordBox Behaviour (see comments in code below):

<PasswordBox>
                    <i:Interaction.Behaviors>
                        <behaviours:PasswordBehavior Password="{Binding Password, Mode=TwoWay}" />
                    </i:Interaction.Behaviors>
                </PasswordBox>

Behaviour:

   public class PasswordBehavior : Behavior<PasswordBox>
{
    public static readonly DependencyProperty PasswordProperty =
    DependencyProperty.Register("Password", typeof(string), typeof(PasswordBehavior), new PropertyMetadata(default(string)));

    private object _value;

    private bool _skipUpdate;

    public string Password
    {
        get { return (string)GetValue(PasswordProperty); }
        set { SetValue(PasswordProperty, value); }
    }

    protected override void OnAttached()
    {
        // in my case on OnAttached() called after OnPropertyChanged
        base.OnAttached();

        AssociatedObject.PasswordChanged += PasswordBox_PasswordChanged;

        // using _value saved before in OnPropertyChanged
        if (_value != null)
        {
            AssociatedObject.Password = _value as string;
        }
    }

    protected override void OnDetaching()
    {
        AssociatedObject.PasswordChanged -= PasswordBox_PasswordChanged;
        base.OnDetaching();
    }

    protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
    {
        // in my case this called before OnAttached(), so that's why AssociatedObject is null on first call
        base.OnPropertyChanged(e);
        if (AssociatedObject == null)
        {
            // so, let'save the value and then reuse it when OnAttached() called
            _value = e.NewValue as string;
            return;
        }

        if (e.Property == PasswordProperty)
        {
            if (!_skipUpdate)
            {
                _skipUpdate = true;
                AssociatedObject.Password = e.NewValue as string;
                _skipUpdate = false;
            }
        }
    }

    private void PasswordBox_PasswordChanged(object sender, RoutedEventArgs e)
    {
        _skipUpdate = true;
        Password = AssociatedObject.Password;
        _skipUpdate = false;
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消