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 7402843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:53:57+00:00 2026-05-29T04:53:57+00:00

I am working with Attached Property in MVVM and encountered a interesting problem. I

  • 0

I am working with Attached Property in MVVM and encountered a interesting problem.

I am making an attached property called WorkType to a button as follow:

public static DependencyProperty WorkTypeProperty = DependencyProperty.RegisterAttached("WorkType",
           typeof(WorkTypeEnum),
           typeof(MyControl),
           new PropertyMetadata(WorkTypeChanged));

public static void SetWorkType(DependencyObject target, WorkTypeEnum value)
{
    target.SetValue(WorkTypeProperty, value);
}

public static WorkTypeEnum GetWorkType(DependencyObject target)
{
    return (WorkTypeEnum)target.GetValue(WorkTypeProperty);
}

public static void WorkTypeClick(object sender, MouseButtonEventArgs e)
{
    var control = (Control)sender;
    WorkTypeEnume workType = (WorkTypeEnum)control.GetValue(WorkTypeProperty);
    (Instance of MyControl).DoWork(workType); ??? <--How to know the instance of MyControl?
}

private static void WorkTypeChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
{
    var control = target as Control;
    if (control != null)
    {
        if ((e.NewValue != null) && (e.OldValue == null))
        {
            control.MouseDown += WorkTypeClick;
        }
        else if ((e.NewValue == null) && (e.OldValue != null))
        {
            control.MouseDown -= WorkTypeClick;
        }
    }
}

I am wondering how could I bind the WorkType to a button so that it will execute the instance of MyControl.DoWork(WorkTypeEnum workType)?

Is there anyway I can assign the instance of MyControl to the button?

Thank you so much!

  • 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-05-29T04:53:58+00:00Added an answer on May 29, 2026 at 4:53 am

    You are using an attached property, there is no instance of MyControl. If you are looking to pass parameters when a button is pressed, it might be worth look at using Commands.

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

Sidebar

Related Questions

I try to use binding with an attached property. But can't get it working.
I'm working on a project for my companies intranet which requires that multiple attached
Here is my working page . I attached buttons to white bishop and you
I've run into an odd problem with attached properties where when I assign the
Hopefully I can articulate this problem well. I'm working on a simple audio player
I'm working on a case logging system. Each case can have comments attached, so
I'm creating an attached behavior in order to set a regular property of a
I'm creating attached property. My attached class is helper:FocusDetail and has 2 property. second
Working with dates in ruby and rails on windows, I'm having problems with pre-epoch
Working with a SqlCommand in C# I've created a query that contains a IN

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.