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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:19:42+00:00 2026-06-14T08:19:42+00:00

I attached behavior (Behavior in Blend SDK) to a UIElement as I generated it

  • 0

I attached behavior (Behavior in Blend SDK) to a UIElement as I generated it in run time. I also had two buttons to trigger Detach() and Attach() – basically to enable or disable the behavior.

The problem is: after I Detach() the behavior, Attach() failed to restore the behavior to the dependency object, which then remained behavior-less:

// Declare the dependency object and behavior at class scope 
MyControl c = new MyControl();
MyBehavior b = new MyBehavior();

// Function that generate UI Controls with behavior attached
b.Attach(c);

// Function that detach behavior from dependency object
b.Detach();

// Function that re-attach behavior to the same object after it has been detached
b.Attach(c); // <-- Do not see the behavior...

Why is the behavior not re-attaching? And is there a solution or work-around to switch on or off a behavior?

  • 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-14T08:19:43+00:00Added an answer on June 14, 2026 at 8:19 am

    It seems the problem is specific of your behavior’s logic. In the following test the behavior reattaches without problems:

    public class ColorBehavior : Behavior<Border>
    {
        public Brush OriginalBrush { get; set; }
    
        protected override void OnAttached()
        {
            base.OnAttached();
    
            this.OriginalBrush = this.AssociatedObject.Background;
            this.AssociatedObject.Background = Brushes.CadetBlue;
        }
    
        protected override void OnDetaching()
        {
            base.OnDetaching();
            this.AssociatedObject.Background = this.OriginalBrush;
        }
    }
    
    public partial class MainWindow : Window
    {
        private ColorBehavior behavior = new ColorBehavior();
        private bool isAttached;
    
        public MainWindow()
        {
            InitializeComponent();
        }
    
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (!this.isAttached)
            {
                this.behavior.Attach(this.Border);
                this.isAttached = true;
            }
            else
            {
                this.behavior.Detach();
                this.isAttached = false;
            }
        }
    }
    
    <Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
        xmlns:local="clr-namespace:WpfApplication1"
        Title="MainWindow"
        Width="525"
        Height="350">
    <Grid>
        <Border x:Name="Border" Background="Red" />
        <Button Width="50"
                Height="20"
                Click="Button_Click"
                Content="Hey" />
    </Grid>
    

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

Sidebar

Related Questions

I've written an Interactivity Behavior (from Blend SDK) , which can be attached to
I have a attached behavior issue. When I attach the behavior to my textbox
At run time, based on user behavior and history, I need to perform a
I've implemented the Attached Behavior Command pattern which compiles and runs fine , but
I am trying to create an attached behavior that can be applied to a
I've implemented the attached command behavior pattern found here and it works well to
I use an attached behavior that allows a DoubleClick event to be wired to
I have an attached behavior defined thusly,.. public static class FileBrowserBehaviour { public static
trying my first attached behavior: I want to bind the TextSelection of the RichTextBox
My goal is to create a reusable Attached Behavior for a FlowDocumentScrollViewer, so that

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.