PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/V1/trunk/Source/CAL/Composite.Wpf/Regions/ItemMetadata.cs

#
C# | 99 lines | 41 code | 10 blank | 48 comment | 4 complexity | 4453bcd9df6a1d21f3e116bb30336546 MD5 | raw file
  1. //===============================================================================
  2. // Microsoft patterns & practices
  3. // Composite Application Guidance for Windows Presentation Foundation
  4. //===============================================================================
  5. // Copyright (c) Microsoft Corporation. All rights reserved.
  6. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
  7. // OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
  8. // LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  9. // FITNESS FOR A PARTICULAR PURPOSE.
  10. //===============================================================================
  11. // The example companies, organizations, products, domain names,
  12. // e-mail addresses, logos, people, places, and events depicted
  13. // herein are fictitious. No association with any real company,
  14. // organization, product, domain name, email address, logo, person,
  15. // places, or events is intended or should be inferred.
  16. //===============================================================================
  17. using System;
  18. using System.Windows;
  19. namespace Microsoft.Practices.Composite.Wpf.Regions
  20. {
  21. /// <summary>
  22. /// Defines a class that wraps an item and adds metadata for it.
  23. /// </summary>
  24. public class ItemMetadata : DependencyObject
  25. {
  26. /// <summary>
  27. /// The name of the wrapped item.
  28. /// </summary>
  29. public static readonly DependencyProperty NameProperty =
  30. DependencyProperty.Register("Name", typeof(string), typeof(ItemMetadata));
  31. /// <summary>
  32. /// Value indicating whether the wrapped item is considered active.
  33. /// </summary>
  34. public static readonly DependencyProperty IsActiveProperty =
  35. DependencyProperty.Register("IsActive", typeof(bool), typeof(ItemMetadata), new PropertyMetadata(DependencyPropertyChanged));
  36. /// <summary>
  37. /// Initializes a new instance of <see cref="ItemMetadata"/>.
  38. /// </summary>
  39. /// <param name="item">The item to wrap.</param>
  40. public ItemMetadata(object item)
  41. {
  42. //check for null
  43. this.Item = item;
  44. }
  45. /// <summary>
  46. /// Gets the wrapped item.
  47. /// </summary>
  48. /// <value>The wrapped item.</value>
  49. public object Item { get; private set; }
  50. /// <summary>
  51. /// Gets or sets a name for the wrapped item.
  52. /// </summary>
  53. /// <value>The name of the wrapped item.</value>
  54. public string Name
  55. {
  56. get { return (string)GetValue(NameProperty); }
  57. set { SetValue(NameProperty, value); }
  58. }
  59. /// <summary>
  60. /// Gets or sets a value indicating whether the wrapped item is considered active.
  61. /// </summary>
  62. /// <value><see langword="true" /> if the item should be considered active; otherwise <see langword="false" />.</value>
  63. public bool IsActive
  64. {
  65. get { return (bool)GetValue(IsActiveProperty); }
  66. set { SetValue(IsActiveProperty, value); }
  67. }
  68. /// <summary>
  69. /// Occurs when metadata on the item changes.
  70. /// </summary>
  71. public event EventHandler MetadataChanged;
  72. /// <summary>
  73. /// Explicitly invokes <see cref="MetadataChanged"/> to notify listeners.
  74. /// </summary>
  75. public void InvokeMetadataChanged()
  76. {
  77. EventHandler metadataChangedHandler = MetadataChanged;
  78. if (metadataChangedHandler != null) metadataChangedHandler(this, EventArgs.Empty);
  79. }
  80. private static void DependencyPropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args)
  81. {
  82. ItemMetadata itemMetadata = dependencyObject as ItemMetadata;
  83. if (itemMetadata != null)
  84. {
  85. itemMetadata.InvokeMetadataChanged();
  86. }
  87. }
  88. }
  89. }