// (c) Copyright Microsoft Corporation. // This source is subject to the Microsoft Public License (Ms-PL). // Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details. // All other rights reserved. using System.Collections.Specialized; using System.Diagnostics.CodeAnalysis; using System.Windows.Data; namespace System.Windows.Controls.DataVisualization { /// /// Represents a control that displays a list of items and has a title. /// /// Preview [StyleTypedProperty(Property = "ItemContainerStyle", StyleTargetType = typeof(ContentPresenter))] [StyleTypedProperty(Property = "TitleStyle", StyleTargetType = typeof(Title))] public partial class Legend : HeaderedItemsControl { #if !SILVERLIGHT /// /// Initializes the static members of the Legend class. /// [SuppressMessage("Microsoft.Performance", "CA1810:InitializeReferenceTypeStaticFieldsInline", Justification = "Dependency properties are initialized in-line.")] static Legend() { DefaultStyleKeyProperty.OverrideMetadata(typeof(Legend), new FrameworkPropertyMetadata(typeof(Legend))); } #endif /// /// Initializes a new instance of the Legend class. /// public Legend() { #if SILVERLIGHT DefaultStyleKey = typeof(Legend); #endif // By default, the Visibility property should follow ContentVisibility - but users can override it SetBinding(VisibilityProperty, new Binding("ContentVisibility") { Source = this }); } /// /// Gets or sets the Style of the ISeriesHost's Title. /// public Style TitleStyle { get { return GetValue(TitleStyleProperty) as Style; } set { SetValue(TitleStyleProperty, value); } } /// /// Identifies the TitleStyle dependency property. /// public static readonly DependencyProperty TitleStyleProperty = DependencyProperty.Register( "TitleStyle", typeof(Style), typeof(Legend), null); /// /// Gets the Visibility of the Legend's content (title and items). /// public Visibility ContentVisibility { get { return (Visibility)GetValue(ContentVisibilityProperty); } protected set { SetValue(ContentVisibilityProperty, value); } } /// /// Identifies the ContentVisibility dependency property. /// public static readonly DependencyProperty ContentVisibilityProperty = DependencyProperty.Register( "ContentVisibility", typeof(Visibility), typeof(Legend), null); /// /// Handles the OnHeaderChanged event for HeaderedItemsControl. /// /// Old header. /// New header. protected override void OnHeaderChanged(object oldHeader, object newHeader) { base.OnHeaderChanged(oldHeader, newHeader); UpdateContentVisibility(); } /// /// Handles the CollectionChanged event for HeaderedItemsControl's ItemsSource. /// /// Event arguments. protected override void OnItemsChanged(NotifyCollectionChangedEventArgs e) { base.OnItemsChanged(e); UpdateContentVisibility(); } /// /// Updates the ContentVisibility property to reflect the presence of content. /// private void UpdateContentVisibility() { ContentVisibility = (Header != null || Items.Count > 0) ? Visibility.Visible : Visibility.Collapsed; } } }