PageRenderTime 14ms CodeModel.GetById 11ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 0ms

/V4/StockTrader RI/Silverlight/StockTraderRI.Infrastructure.Silverlight/DependencyPropertyHelper.cs

#
C# | 51 lines | 22 code | 3 blank | 26 comment | 4 complexity | 416bd0dd5f7b2531ab73656c14fdb744 MD5 | raw file
 1//===================================================================================
 2// Microsoft patterns & practices
 3// Composite Application Guidance for Windows Presentation Foundation and Silverlight
 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//===================================================================================
17using System.Windows;
18using System;
19
20namespace StockTraderRI.Infrastructure
21{
22    /// <summary>
23    /// Defines helper methods for dependency properties.
24    /// </summary>
25    public static class DependencyPropertyHelper
26    {
27        /// <summary>
28        /// Gets or, if it is not defined sets, the <paramref name="property"/> value on the <paramref name="dependencyObject"/> object.
29        /// </summary>
30        /// <typeparam name="T"><see cref="System.Type"/> of the <see cref="DependencyProperty"/>.</typeparam>
31        /// <param name="dependencyObject">Object on which the <paramref name="property"/> is set.</param>
32        /// <param name="property">Property whose value will be retrieved.</param>
33        /// <returns>Value of <paramref name="property"/> on <paramref name="dependencyObject"/>.</returns>
34        public static T GetOrAddValue<T>(DependencyObject dependencyObject, DependencyProperty property) where T : class, new()
35        {
36            if (dependencyObject == null)
37            {
38                throw new ArgumentNullException("dependencyObject");
39            }
40
41            T value = dependencyObject.GetValue(property) as T;
42            if (value == null)
43            {
44                value = new T();
45                dependencyObject.SetValue(property, value);
46            }
47
48            return value;
49        }
50    }
51}