Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8856087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:16:52+00:00 2026-06-14T14:16:52+00:00

I have a working attached behavior which I would like to add a DP

  • 0

I have a working attached behavior which I would like to add a DP to. I can set the property in XAML but it is null when I try to access it.

What is the fix?

Cheers,
Berryl

xaml

<Button Command="{Binding ContactCommand}" local:ContactCommandBehavior.ResourceKey="blah" >
    <i:Interaction.Behaviors>
        <local:ContactCommandBehavior />
    </i:Interaction.Behaviors>
</Button>

Behavior code

internal class ContactCommandBehavior : Behavior<ContentControl>
{
    ...

    public static readonly DependencyProperty ResourceKeyProperty = 
        DependencyProperty.RegisterAttached("ResourceKey", typeof(string), typeof(ContactCommandBehavior));

    public static string GetResourceKey(FrameworkElement element)
    {
        return (string)element.GetValue(ResourceKeyProperty);
    }

    public static void SetResourceKey(FrameworkElement element, string value)
    {
        element.SetValue(ResourceKeyProperty, value);
    }

    private void SetProperties(IHaveDisplayName detailVm)
    {

        //************ 
        var key = GetResourceKey(AssociatedObject);
        //************ 
        ....
    }

}

Edit for HighCore.

I change the code as follows, changing RegisterAttached to Register and making the property non-static. The value is still null when I try to get at it though

public static readonly DependencyProperty ResourceKeyProperty =
    DependencyProperty.Register("ResourceKey", typeof (string), typeof (ContactCommandBehavior));

public string ResourceKey
{
    get { return (string)GetValue(ResourceKeyProperty); }
    set { SetValue(ResourceKeyProperty, value); }
}

protected override void OnAttached() {
    base.OnAttached();
    if (AssociatedObject == null)
        throw new InvalidOperationException("AssociatedObject must not be null");

    AssociatedObject.DataContextChanged += OnDataContextChanged;
    CultureManager.UICultureChanged += OnCultureChanged;
}

private void OnDataContextChanged(object sender, DependencyPropertyChangedEventArgs e) {
    // do some setup stuff
    SetProperties(vm)
}

private void SetProperties(IHaveDisplayName detailVm)
{
    ////////////////////////////////
    var key = ResourceKey.Replace(TOKEN, cmType);
    /////////////////////////////////
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T14:16:53+00:00Added an answer on June 14, 2026 at 2:16 pm

    Use a regular DependencyProperty in the Behavior instead of an attached one, then you can do

    <Button Command="{Binding ContactCommand}">
        <i:Interaction.Behaviors>
            <local:ContactCommandBehavior ResourceKey="blah"/>
        </i:Interaction.Behaviors>
    </Button>
    

    which is a much nicer syntax. Also, make sure the code you try to read these properties only AFTER OnAttached() has ocurred.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been working with a iPhone app that I would like to have
I have implemented a crypt behavior class that can be attached to a AR
I have been working very hard on several very long views which are attached
I'm working on a little blog software, and I'd like to have tags attached
I have a ContextMenuStrip attached to a list view, and it's working great, but
I have working website in PHP with a MySQL backend which has several tables
I have a third party library which I finally have working within my MonoTouch
I have a working solution to my problem, but I will not be able
I have an attached behavior to an ItemsControl that scrolls down to the bottom
I have been working on this for hours, and I just can't figure it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.