ResourceDictionary WPF中样式中的交互触发器

我有一个ComboBox ,我需要在我的应用程序中的几个地方使用,所以我在ResourceDictionary设置了该ComboBox大部分属性,并将其用作我需要它的Style。

ComboBox风格是:

      

我在我的XAML中使用它是这样的:

        

我想将交互触发器代码也移动到ResourceDictionary ,所以我不需要在所有的xamls中编写它。 有可能吗?

据我所知, Interaction.Triggers不能分别在Style中和ResourceDictionary中应用。 但您可以这样做,将ComboBox确定为x:Shared="False"的资源,并将其引用为ContentControl如下所示:

              

x:Shared="True"默认情况下,一个Style对所有人都是通用的 – 在这种情况下,系统会对重复的Content发誓。 当x:Shared="False"为何时创建每个元素的样式。 从MSDN引用:

设置为false时 ,修改WPF资源检索行为,以便对属性资源的请求为每个请求创建new instance ,而不是为所有请求共享同一实例。

有关更多信息,请参阅:

MSDN: x:Shared Attribute

Edit: alternative solution

Here ,Mr.Vspivak发布了一个解决方案,允许您轻松设置Style中的Interaction.Triggers

例:

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; } } } 

有关详细信息,请参阅此链接:

Using Interactivity Behaviors and Actions in WPF/Silverlight Styles

我通常使用Silverlight,因此我不确定以下方法在WPF中是否合理:您可以将xaml拉入UserControl ,例如BranchSelection.xaml

          

并像这样使用它: