Add to Faves Add to MyAOL Add to Simpy Add to Delicious Add to Live Add to Digg Add to Newsvine Add to Reddit Add to Multiply Add to Blogmarks Add to Yahoo MyWeb Add to Slashdot Add to Mister Wong Add to Spurl Add to Furl Add to Link-a-Gogo Add to Yahoo Bookmarks Add to Twitter Add to Facebook Add to Diigo Add to Mixx Add to Segnalo Add to StumbleUpon Add to Ask Add to Backflip Add to Terchnorati Add to Google Bookmarks Add to MySpace
BlogProductsOpen SourceForumsAbout
Blog > October 2010 > Attaching Behaviors from the Expression Blend SDK using Styles (Part 3)

Attaching Behaviors from the Expression Blend SDK using Styles (Part 3)

In the previous posts in this series (Attaching Behaviors from the Expression Blend SDK using Styles and Attaching Behaviors from the Expression Blend SDK using Styles (Part 2)), I introduced the reader to a new attached property that gives the ability to attach a collection of behaviors to an item through a style.

I received feedback from two developers regarding a bug in the code that was in Part 2, and as a result I wanted to post a corrected version of the code.

Rather than do a walk through, here is the full, revised version of the StylizedBehaviors class:

using System.Windows;
using System.Windows.Interactivity;
#endregion

namespace LivingAgile.Common.Presentation.WPF.Behaviors
{
    public class StylizedBehaviors
    {
        #region Fields (private)
        private static readonly DependencyProperty OriginalBehaviorProperty =
            DependencyProperty.RegisterAttached(
                @"OriginalBehaviorInternal"typeof (Behavior), typeof (StylizedBehaviors), new UIPropertyMetadata(null));
        #endregion

        #region Fields (public)
        public static readonly DependencyProperty BehaviorsProperty = DependencyProperty.RegisterAttached(
            @"Behaviors",
            typeof (StylizedBehaviorCollection),
            typeof (StylizedBehaviors),
            new FrameworkPropertyMetadata(null, OnPropertyChanged));
        #endregion

        #region Static Methods (public)
        public static StylizedBehaviorCollection GetBehaviors(DependencyObject uie)
        {
            return (StylizedBehaviorCollection) uie.GetValue(BehaviorsProperty);
        }

        public static void SetBehaviors(DependencyObject uie, StylizedBehaviorCollection value)
        {
            uie.SetValue(BehaviorsProperty, value);
        }
        #endregion

        #region Static Methods (private)
        private static Behavior GetOriginalBehavior(DependencyObject obj)
        {
            return obj.GetValue(OriginalBehaviorProperty) as Behavior;
        }

        private static int GetIndexOf(BehaviorCollection itemBehaviors, Behavior behavior)
        {
            int index = -1;

            Behavior orignalBehavior = GetOriginalBehavior(behavior);

            for (int i = 0; i < itemBehaviors.Count; i++)
            {
                Behavior currentBehavior = itemBehaviors[i];

                if (currentBehavior == behavior
                    || currentBehavior == orignalBehavior)
                {
                    index = i;
                    break;
                }

                Behavior currentOrignalBehavior = GetOriginalBehavior(currentBehavior);

                if (currentOrignalBehavior == behavior
                    || currentOrignalBehavior == orignalBehavior)
                {
                    index = i;
                    break;
                }
            }

            return index;
        }

        private static void OnPropertyChanged(DependencyObject dpo, DependencyPropertyChangedEventArgs e)
        {
            var uie = dpo as UIElement;

            if (uie == null)
            {
                return;
            }

            BehaviorCollection itemBehaviors = Interaction.GetBehaviors(uie);

            var newBehaviors = e.NewValue as StylizedBehaviorCollection;
            var oldBehaviors = e.OldValue as StylizedBehaviorCollection;

            if (newBehaviors == oldBehaviors)
            {
                return;
            }

            if (oldBehaviors != null)
            {
                foreach (var behavior in oldBehaviors)
                {
                    int index = GetIndexOf(itemBehaviors, behavior);

                    if (index >= 0)
                    {
                        itemBehaviors.RemoveAt(index);
                    }
                }
            }

            if (newBehaviors != null)
            {
                foreach (var behavior in newBehaviors)
                {
                    int index = GetIndexOf(itemBehaviors, behavior);

                    if (index < 0)
                    {
                        var clone = (Behavior) behavior.Clone();
                        SetOriginalBehavior(clone, behavior);
                        itemBehaviors.Add(clone);
                    }
                }
            }
        }

        private static void SetOriginalBehavior(DependencyObject obj, Behavior value)
        {
            obj.SetValue(OriginalBehaviorProperty, value);
        }
        #endregion
    }
}

The interested reader can refer to the comments on the previous posts to see what motivated the changes.

Posted: 10/25/2010 11:57:21 AM by Mark Smeltzer | with 0 comments
Filed under: .NET4.0, Behaviors, Expression, OpenSource, Styles, VS, VS2010, WPF, XAML, .NET


Comments
Blog post currently doesn't have any comments.
Leave comment Subscribe



 Security code

Recent Comments

10/25/2010
Mark Smeltzer
Beej, I received similar feedback from Antoine. Based on...

10/25/2010
Beej
In my WPF 4 project I was getting error: "Cannot set a...

10/25/2010
Saheer
Hi Mark, Will this work in Silverlight? I think Freezable...

8/24/2010
Mark Smeltzer
When you say that you are getting errors, are they compilat...

8/24/2010
Antoine
Thanks for your quick reply! I renamed the class to Inte...

8/23/2010
Mark Smeltzer
I have not had any problems with that. Could you post your ...

8/23/2010
Antoine
Hi, Thanks a lot for this, I found it interesting and tr...

7/31/2010
Blog - Attaching Behaviors from the Expression Blend SDK using Styles (Part 2)
In the previous post in this series, I introduce the reader...

6/17/2010
Mark Smeltzer
Werner, The latest release, 0.9.7.4, should fix the prob...

5/31/2010
Mark Smeltzer
Werner, Are you using AnkhSVN? I have created a patch...

|< <  1   2   > >| Results 1 - 10 of 11

Syndication

RSS
This web site uses Kentico CMS, the content management system for ASP.NET developers.