vspivak

Just another WordPress.com site

Problem with Microsoft.Expression.Interactivity.Core.DataTrigger

It looks like DataTrigger have some annoying bug – it does not respect FrameworkElement loading sequence and this way misses those bindings that are, for example, resource bound.

This will not work


<i:Interaction.Triggers>
      <Core:DataTrigger Binding="{Binding Source={StaticResource MeasurementData}, Path=IsMultiSite}" 
                                  Value="False" Comparison="Equal">
               <Core:ChangePropertyAction PropertyName="Visibility" Value="Collapsed" TargetName="MyElement"/>
      </Core:DataTrigger>
</i:Interaction.Triggers>

The solution is slightly extended version of DataTrigger


using System.Windows;

namespace Microsoft.Expression.Interactivity.Core
{
    public class XDataTrigger:DataTrigger
    {
        public bool RespectLoadedEvent
        {
            get { return (bool) GetValue(RespectLoadedEventProperty); }
            set { SetValue(RespectLoadedEventProperty, value); }
        }

        public static readonly DependencyProperty RespectLoadedEventProperty =
            DependencyProperty.Register("RespectLoadedEvent", typeof (bool), typeof (XDataTrigger), new PropertyMetadata(true, OnRespectLoadedEventChanged));

        private static void OnRespectLoadedEventChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            
        }

        protected override void OnAttached()
        {   
            if(AssociatedObject is FrameworkElement && RespectLoadedEvent)
                ((FrameworkElement)AssociatedObject).Loaded += XDataTriggerLoaded;
            base.OnAttached();
        }

        protected override void OnDetaching()
        {
            base.OnDetaching();
            if (AssociatedObject is FrameworkElement)
                ((FrameworkElement)AssociatedObject).Loaded -= XDataTriggerLoaded;

        }

        void XDataTriggerLoaded(object sender, RoutedEventArgs e)
        {
           EvaluateBindingChange(null);
        }
    }
}

Enjoy!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: