PageRenderTime 19ms CodeModel.GetById 11ms app.highlight 4ms RepoModel.GetById 2ms 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
18using System;
19using System.Windows;
20
21namespace Microsoft.Practices.Composite.Wpf.Regions
22{
23    /// <summary>
24    /// Defines a class that wraps an item and adds metadata for it.
25    /// </summary>
26    public class ItemMetadata : DependencyObject
27    {
28        /// <summary>
29        /// The name of the wrapped item.
30        /// </summary>
31        public static readonly DependencyProperty NameProperty =
32            DependencyProperty.Register("Name", typeof(string), typeof(ItemMetadata));
33
34        /// <summary>
35        /// Value indicating whether the wrapped item is considered active.
36        /// </summary>
37        public static readonly DependencyProperty IsActiveProperty =
38            DependencyProperty.Register("IsActive", typeof(bool), typeof(ItemMetadata), new PropertyMetadata(DependencyPropertyChanged));
39
40        /// <summary>
41        /// Initializes a new instance of <see cref="ItemMetadata"/>.
42        /// </summary>
43        /// <param name="item">The item to wrap.</param>
44        public ItemMetadata(object item)
45        {
46            //check for null
47            this.Item = item;
48        }
49
50        /// <summary>
51        /// Gets the wrapped item.
52        /// </summary>
53        /// <value>The wrapped item.</value>
54        public object Item { get; private set; }
55
56        /// <summary>
57        /// Gets or sets a name for the wrapped item.
58        /// </summary>
59        /// <value>The name of the wrapped item.</value>
60        public string Name
61        {
62            get { return (string)GetValue(NameProperty); }
63            set { SetValue(NameProperty, value); }
64        }
65
66        /// <summary>
67        /// Gets or sets a value indicating whether the wrapped item is considered active.
68        /// </summary>
69        /// <value><see langword="true" /> if the item should be considered active; otherwise <see langword="false" />.</value>
70        public bool IsActive
71        {
72            get { return (bool)GetValue(IsActiveProperty); }
73            set { SetValue(IsActiveProperty, value); }
74        }
75
76        /// <summary>
77        /// Occurs when metadata on the item changes.
78        /// </summary>
79        public event EventHandler MetadataChanged;
80
81        /// <summary>
82        /// Explicitly invokes <see cref="MetadataChanged"/> to notify listeners.
83        /// </summary>
84        public void InvokeMetadataChanged()
85        {
86            EventHandler metadataChangedHandler = MetadataChanged;
87            if (metadataChangedHandler != null) metadataChangedHandler(this, EventArgs.Empty);
88        }
89
90        private static void DependencyPropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args)
91        {
92            ItemMetadata itemMetadata = dependencyObject as ItemMetadata;
93            if (itemMetadata != null)
94            {
95                itemMetadata.InvokeMetadataChanged();
96            }
97        }
98    }
99}