Interaction Triggers in Style in ResourceDictionary WPF

后端 未结 2 1984
面向向阳花
面向向阳花 2020-12-03 00:02

I have a ComboBox which I need to use in several places in my application, so I set most of the properties of that ComboBox in ResourceDictio

2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-03 00:49

    As far as I know, Interaction.Triggers can not be applied in Style, respectively and in a ResourceDictionary. But you can do so, to determine the ComboBox as a resource with x:Shared="False" and reference it for ContentControl like this:

    
        
    
            
                
                    
                
            
        
    
    
    
        
    
        
    
    

    When x:Shared="True" by default then one Style is common to all - in this case, the system swears on the duplicate Content. When x:Shared="False" when is created Style for each element whenever it its request. Quote from MSDN:

    When set to false, modifies WPF resource-retrieval behavior so that requests for the attributed resource create a new instance for each request instead of sharing the same instance for all requests.

    For more information, please see:

    MSDN: x:Shared Attribute

    Edit: alternative solution

    Here, Mr.Vspivak published a solution that allows you easily set the Interaction.Triggers in Style.

    Example:

    MainWindow.xaml

    
    
        
            
        
    
              
            
                
                
            
    
            
    
            
    
                
                    
                
            
        
    
    

    InteractivityHelper.cs

    /// 
    ///  for InteractivityElements instance
    /// Subclassed for forward compatibility, perhaps one day  
    /// will not be partially internal
    /// 
    public class InteractivityTemplate : DataTemplate
    {
    
    }
    
    /// 
    /// Holder for interactivity entries
    /// 
    public class InteractivityItems : FrameworkElement
    {
        private List _behaviors;
        private List _triggers;
    
        /// 
        /// Storage for triggers
        /// 
        public List Triggers
        {
            get
            {
                if (_triggers == null)
                    _triggers = new List();
                return _triggers;
            }
        }
    
        /// 
        /// Storage for Behaviors
        /// 
        public List Behaviors
        {
            get
            {
                if (_behaviors == null)
                    _behaviors = new List();
                return _behaviors;
            }
        }
    
        #region Template attached property
    
        public static InteractivityTemplate GetTemplate(DependencyObject obj)
        {
            return (InteractivityTemplate)obj.GetValue(TemplateProperty);
        }
    
        public static void SetTemplate(DependencyObject obj, InteractivityTemplate value)
        {
            obj.SetValue(TemplateProperty, value);
        }
    
        public static readonly DependencyProperty TemplateProperty =
            DependencyProperty.RegisterAttached("Template", 
            typeof(InteractivityTemplate), 
            typeof(InteractivityItems),
            new PropertyMetadata(default(InteractivityTemplate), OnTemplateChanged));
    
        private static void OnTemplateChanged(
            DependencyObject d, 
            DependencyPropertyChangedEventArgs e)
        {
            InteractivityTemplate dt = (InteractivityTemplate)e.NewValue;
    #if(!SILVERLIGHT)
            dt.Seal();
    #endif
            InteractivityItems ih = (InteractivityItems)dt.LoadContent();
            BehaviorCollection bc = Interaction.GetBehaviors(d);
            TriggerCollection tc = Interaction.GetTriggers(d);
    
            foreach (Behavior behavior in ih.Behaviors)
                bc.Add(behavior);
    
            foreach (TriggerBase trigger in ih.Triggers)
                tc.Add(trigger);
        }
    
        #endregion
    }
    

    FlipOnHover.cs

    public class FlipOnHover : Behavior
    {
        protected override void OnAttached()
        {
            AssociatedObject.MouseEnter += AssociatedObject_MouseEnter;
            AssociatedObject.MouseLeave += AssociatedObject_MouseLeave;
            Transform t = AssociatedObject.RenderTransform;
    
            AssociatedObject.RenderTransform = new TransformGroup();
            ((TransformGroup)AssociatedObject.RenderTransform).Children.Add(t);
            ((TransformGroup)AssociatedObject.RenderTransform).Children.Add(new ScaleTransform());
            base.OnAttached();
        }
    
        void AssociatedObject_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            ((ScaleTransform)((TransformGroup)AssociatedObject.RenderTransform).Children[1]).ScaleY = 1;
        }
    
        void AssociatedObject_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            ((ScaleTransform)((TransformGroup)AssociatedObject.RenderTransform).Children[1]).CenterX = AssociatedObject.ActualWidth / 2;
            ((ScaleTransform)((TransformGroup)AssociatedObject.RenderTransform).Children[1]).CenterY = AssociatedObject.ActualHeight / 2;
            ((ScaleTransform)((TransformGroup)AssociatedObject.RenderTransform).Children[1]).ScaleY=-1;
        }
    
        protected override void OnDetaching()
        {
            base.OnDetaching();
            AssociatedObject.MouseEnter -= AssociatedObject_MouseEnter;
            AssociatedObject.MouseLeave -= AssociatedObject_MouseLeave;
        }
    }
    

    ViewModel.cs

    public class ViewModel
    {
        private ObservableCollection _dataSource = new ObservableCollection();
    
        public ViewModel()
        {
            _dataSource.Add("Cat");
            _dataSource.Add("Dog");
            _dataSource.Add("Mouse");
            _dataSource.Add("Owl");
            _dataSource.Add("Rabbit");
        }
    
        public IEnumerable DataSource
        {
            get { return _dataSource; }
        }
    }
    

    For more info, see this link:

    Using Interactivity Behaviors and Actions in WPF/Silverlight Styles

提交回复
热议问题