using System.Windows; using System.Windows.Controls; using Prism.Events; using Prism.Ioc; using Microsoft.Windows.Controls.Ribbon; using Microsoft.Xaml.Behaviors; namespace DTS.Common.RibbonControl { public class RibbonControlSelectionChangeBehavior : Behavior { public static readonly DependencyProperty TargetRibbonProperty = DependencyProperty.Register("TargetRibbonControl", typeof(Ribbon), typeof(RibbonControlSelectionChangeBehavior), new PropertyMetadata(null)); private IEventAggregator _eventAggregator; public Ribbon TargetRibbonControl { get => (Ribbon)GetValue(TargetRibbonProperty); set => SetValue(TargetRibbonProperty, value); } protected override void OnAttached() { base.OnAttached(); AssociatedObject.SelectionChanged += Ribbon_SelectionChanged; if (_eventAggregator == null) _eventAggregator = ContainerLocator.Container.Resolve(); } protected override void OnDetaching() { base.OnDetaching(); AssociatedObject.SelectionChanged -= Ribbon_SelectionChanged; } void Ribbon_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (e.AddedItems.Count > 0) _eventAggregator.GetEvent().Publish(new RibbonControlSelectionEventArgs(RibbonControlOperation.AddedItem, e.AddedItems[0])); if (e.RemovedItems.Count > 0) _eventAggregator.GetEvent().Publish(new RibbonControlSelectionEventArgs(RibbonControlOperation.RemovedItem, e.RemovedItems[0])); } } }