Skip to content

Instantly share code, notes, and snippets.

@holyketzer
Created October 2, 2013 11:33
Show Gist options
  • Save holyketzer/6792382 to your computer and use it in GitHub Desktop.
Save holyketzer/6792382 to your computer and use it in GitHub Desktop.
Implementation of SCOM behavior that add menu items (tasks) under "gear box"
<ComponentBehavior ID="Microsoft.SystemCenter.Visualization.Component.Library.ObjectHealthWidgetShowHideMonitorsBehavior"
BehaviorTypeId="Microsoft.SystemCenter.Visualization.Component.Library.ShowHideMonitorsBehaviours"
ComponentTypeId="Here is your target component (can be visual or not)"
Accessibility="Internal">
<Bindings>
</Bindings>
</ComponentBehavior>
</ComponentBehaviors>
<BehaviorTypes>
<BehaviorType ID="Microsoft.SystemCenter.Visualization.Component.Library.ShowHideMonitorsBehaviours" Accessibility="Public" />
</BehaviorTypes>
<BehaviorImplementations>
<BehaviorImplementation ID="Microsoft.SystemCenter.Visualization.Component.Library.ShowHideMonitorsBehavioursImplementation"
TypeId="Microsoft.SystemCenter.Visualization.Component.Library.ShowHideMonitorsBehaviours"
Platform="Wpf, Silverlight"
Accessibility="Internal">
<Unit>
<ContractFactory>Microsoft.SystemCenter.Visualization.Component.Library.Behaviours.ShowHideMonitorsBehaviours</ContractFactory>
</Unit>
</BehaviorImplementation>
</BehaviorImplementations>
</Presentation>
using System;
using System.ComponentModel;
using System.ComponentModel.Composition;
using System.Reactive;
using System.Threading;
using Microsoft.EnterpriseManagement.CompositionEngine;
using Microsoft.EnterpriseManagement.Monitoring.Components;
using Microsoft.EnterpriseManagement.Monitoring.Components.Tasks;
using Microsoft.Practices.Unity;
namespace Microsoft.SystemCenter.Visualization.Component.Library.Behaviours
{
[Export]
[PartCreationPolicy(CreationPolicy.NonShared)]
[ComponentBuildUpRequired]
public class ShowHideMonitorsBehaviours : IComponentBehavior, INotifyPropertyChanged
{
private const string TaskServiceName = "$Service/Tasks$";
/// <summary>
/// Gets or sets the container.
/// </summary>
/// <value>The container.</value>
[Dependency]
public IUnityContainer Container { get; set; }
#region IComponentBehavior members
public IObservable<Unit> DecorateBindings(ComponentType componentType, DataContext dataContext)
{
var simpleSubject = new SimpleSubject<Unit>();
simpleSubject.OnNext(new Unit());
simpleSubject.OnCompleted();
return simpleSubject;
}
public object DecorateComponent(ComponentType componentType, object component, DataContext declarationDataContext, ComponentProcessRequest componentProcessRequest)
{
var syncronizationContext = SynchronizationContext.Current;
var tasks = new TaskGroup(Container) { DisplayName = "ShowHideMonitorsBehaviours" };
var propName = "ShowOnlyUnhealthyMonitors";
if (declarationDataContext.ContainsKey(propName))
{
if (declarationDataContext[propName] == null)
{
declarationDataContext[propName] = false;
}
var showOnlyUnhealthyMonitorsTask = new RegionTask
{
DisplayName = "Show only unhealthy monitors",
Enabled = true,
//Enabled = !(bool)declarationDataContext[propName],
Icon = ComponentResources.AuxNavigationTask_16
};
var showAllMonitorsTask = new RegionTask
{
DisplayName = "Show all monitors",
Enabled = true,
//Enabled = (bool)declarationDataContext[propName],
Icon = ComponentResources.AuxNavigationTask_16
};
showOnlyUnhealthyMonitorsTask.Execute = () =>
{
declarationDataContext[propName] = true;
//showOnlyUnhealthyMonitorsTask.Enabled = false;
//showAllMonitorsTask.Enabled = true;
};
showAllMonitorsTask.Execute = () =>
{
declarationDataContext[propName] = false;
//showAllMonitorsTask.Enabled = false;
//showOnlyUnhealthyMonitorsTask.Enabled = true;
};
tasks.AddTask(showOnlyUnhealthyMonitorsTask);
tasks.AddTask(showAllMonitorsTask);
declarationDataContext[TaskServiceName] = tasks;
}
return component;
}
#endregion
#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChange(string prop)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(prop));
}
}
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment