// ----------------------------------------------------------------------- // // Copyright 2015 MIT Licence. See licence.md for more information. // // ----------------------------------------------------------------------- namespace Perspex.Controls.Mixins { using System; using Perspex.Interactivity; using Primitives; /// /// Adds selectable functionality to control classes. /// /// /// /// The adds behavior to a control which can be /// selected. It adds the following behavior: /// /// /// /// Raises an when the value if /// the IsSelected property changes. /// /// /// Adds a 'selected' class to selected controls. /// /// /// /// Mixins apply themselves to classes and not instances, and as such should be created in /// a static constructor. /// /// public static class SelectableMixin { /// /// Initializes a new instance of the class. /// /// The control type. /// The IsSelected property. public static void Attach(PerspexProperty isSelected) where TControl : class, IControl { Contract.Requires(isSelected != null); isSelected.Changed.Subscribe(x => { var sender = x.Sender as TControl; if (sender != null) { if ((bool)x.NewValue) { sender.Classes.Add("selected"); } else { sender.Classes.Remove("selected"); } sender.RaiseEvent(new RoutedEventArgs { RoutedEvent = SelectingItemsControl.IsSelectedChangedEvent }); } }); } } }