-
Notifications
You must be signed in to change notification settings - Fork 0
WPF
Sevenate edited this page Oct 24, 2018
·
2 revisions
using System.Windows;
using System.Windows.Interactivity;
namespace Fab.Client.Framework.Behaviors
{
/// <summary>
/// Behaviors that could be added from style.
/// </summary>
public class StylizedBehaviors
{
#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 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 = itemBehaviors.IndexOf(behavior);
if (index >= 0)
{
itemBehaviors.RemoveAt(index);
}
}
}
if (newBehaviors != null)
{
foreach (var behavior in newBehaviors)
{
int index = itemBehaviors.IndexOf(behavior);
if (index < 0)
{
itemBehaviors.Add(behavior);
}
}
}
}
#endregion
}
}
using System.Windows.Interactivity;
namespace Fab.Client.Framework.Behaviors
{
/// <summary>
/// Collection of <see cref="StylizedBehaviors"/>.
/// </summary>
public class StylizedBehaviorCollection : FreezableCollection<Behavior>
{
#region Methods (protected)
protected override Freezable CreateInstanceCore()
{
return new StylizedBehaviorCollection();
}
#endregion
}
}
xmlns:WPF="clr-namespace:LivingAgile.Common.WPF"
<Style TargetType="{x:Type Button}">
<Setter Property="WPF:StylizedBehaviors.Behaviors">
<Setter.Value>
<WPF:StylizedBehaviorCollection>
<MyBehaviors:PopOnHoverBehavior />
</WPF:StylizedBehaviorCollection>
</Setter.Value>
</Setter>
</Style>